175

I have a bash script like:

#!/bin/bash

echo Hello world!

How do I execute this in Terminal?

Sohum Sachdev
  • 1,397
  • 1
  • 11
  • 23
Ballon
  • 6,882
  • 18
  • 49
  • 63

9 Answers9

175

Yet another way to execute it (this time without setting execute permissions):

bash /path/to/scriptname
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • had a seperate issue (trying to install anaconda via terminal on mac) and this was the solution. only thing I had to do was close and restart the shell, follow the one step [here](https://stackoverflow.com/questions/40370467/anaconda-not-found-in-zsh/56835357#56835357) (`/(your conda installation path)/bin/conda init zsh`) and then close and reopen the shell one more time – S.Chauhan Jul 20 '22 at 16:43
67

$prompt: /path/to/script and hit enter. Note you need to make sure the script has execute permissions.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • 3
    If you already are in the `/path/to` directory, e.g. with the `cd /path/to` command, you can enter `./script` to run your script.Don't forget, in this case the './' before 'script' – FrViPofm Dec 28 '19 at 10:59
42

cd to the directory that contains the script, or put it in a bin folder that is in your $PATH

then type

./scriptname.sh

if in the same directory or

scriptname.sh

if it's in the bin folder.

John Boker
  • 82,559
  • 17
  • 97
  • 130
  • 12
    This will only work if the script has the execute bit set. That probably needs to be addressed. – Bryan Oakley Feb 01 '10 at 16:15
  • `./scriptname.sh` works for me but `scriptname.sh` gives `scriptname.sh: command not found`. `-rwxr-xr-x` are its permissions. – Spikatrix Apr 21 '17 at 12:24
  • 1
    The advice to `cd` anywhere at all perpetrates another common beginner misunderstanding. Unless the script internally has dependencies which require it to run in a particular directory (like, needing to read a data file which the script inexplicably doesn't provide an option to point to) you should never need to `cd` anywhere to run it, and very often will not want to. – tripleee Mar 25 '19 at 15:55
33

This is an old thread, but I happened across it and I'm surprised nobody has put up a complete answer yet. So here goes...

The Executing a Command Line Script Tutorial!

Q: How do I execute this in Terminal?

The answer is below, but first ... if you are asking this question, here are a few other tidbits to help you on your way:

Confusions and Conflicts:

The Path

  • Understanding The Path (added by tripleee for completeness) is important. The "path" sounds like a Zen-like hacker koan or something, but it is simply a list of directories (folders) that are searched automatically when an unknown command is typed in at the command prompt. Some commands, like ls may be built-in's, but most commands are actually separate small programs. (This is where the "Zen of Unix" comes in ... "(i) Make each program do one thing well.")

Extensions

  • Unlike the old DOS command prompts that a lot of people remember, you do not need an 'extension' (like .sh or .py or anything else), but it helps to keep track of things. It is really only there for humans to use as a reference and most command lines and programs will not care in the least. It won't hurt. If the script name contains an extension, however, you must use it. It is part of the filename.

Changing directories

  • You do not need to be in any certain directory at all for any reason. But if the directory is not on the path (type echo $PATH to see), then you must include it. If you want to run a script from the current directory, use ./ before it. This ./ thing means 'here in the current directory.'

Typing the program name

  • You do not need to type out the name of the program that runs the file (BASH or Python or whatever) unless you want to. It won't hurt, but there are a few times when you may get slightly different results.

SUDO

  • You do not need sudo to do any of this. This command is reserved for running commands as another user or a 'root' (administrator) user. Running scripts with sudo allows much greater danger of screwing things up. So if you don't know the exact reason for using sudo, don't use it. Great post here.

Script location ...

  • A good place to put your scripts is in your ~/bin folder.
  • You can get there by typing
# A good place to put your scripts is in your ~/bin folder.

> cd ~/bin # or cd $HOME/bin

> ls -l

You will see a listing with owners and permissions. You will notice that you 'own' all of the files in this directory. You have full control over this directory and nobody else can easily modify it.

If it does not exist, you can create one:

> mkdir -p ~/bin && cd ~/bin
> pwd

/Users/Userxxxx/bin


A: To "execute this script" from the terminal on a Unix/Linux type system, you have to do three things:

1. Tell the system the location of the script. (pick one)

# type the name of the script with the full path
> /path/to/script.sh

# execute the script from the directory it is in
> ./script.sh

# place the script in a directory that is on the PATH
> script.sh

# ... to see the list of directories in the path, use:
> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# ... or for a list that is easier to read:
> echo -e ${PATH//:/\\n}
# or
> printf "%b" "${PATH//:/\\n}"
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

2. Tell the system that the script has permission to execute. (pick one)

# set the 'execute' permissions on the script
> chmod +x /path/to/script.sh

# using specific permissions instead
# FYI, this makes these scripts inaccessible by ANYONE but an administrator
> chmod 700 /path/to/script.sh

# set all files in your script directory to execute permissions
> chmod +x ~/bin/*

There is a great discussion of permissions with a cool chart here.

3. Tell the system the type of script. (pick one)

  • Type the name of the program before the script. (Note: when using this method, the execute(chmod thing above) is not required
> bash /path/to/script.sh
... 

> php /path/to/script.php
... 

> python3 /path/to/script.py
...
  • Use a shebang, which I see you have (#!/bin/bash) in your example. If you have that as the first line of your script, the system will use that program to execute the script. No need for typing programs or using extensions.
  • Use a "portable" shebang. You can also have the system choose the version of the program that is first in the PATH by using #!/usr/bin/env followed by the program name (e.g. #!/usr/bin/env bash or #!/usr/bin/env python3). There are pros and cons as thoroughly discussed here.

Note: This "portable" shebang may not be as portable as it seems. As with anything over 50 years old and steeped in numerous options that never work out quite the way you expect them ... there is a heated debate. The most recent one I saw that is actually quite different from most ideas is the "portable" perl-bang:

#!/bin/sh
exec perl -x "$0" "$@"
#!perl
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Michael Treanor
  • 585
  • 8
  • 9
  • 2
    Pretty good summary. Maybe in the last example remind people that `script.sh` and `script.php` are the literal file names of these scripts, and that if you put an extension in the file name, you need to include it when you run the script (and vice versa; if the script doesn't have an extension, don't put one). This is unlike e.g. DOS where you could omit the `.bat` or `.exe` from the file name, and files needed to have an extension from a small set in order to be considered executable. – tripleee Mar 25 '19 at 15:59
  • 1
    Another common beginner mistake is misunderstanding the difference between `path`, `/path`, `./path`, and `~/path`. I guess you probably don't want to explain it here, but maybe link to an explanation such as ... oh dang, I had to post [this.](/a/55342466/874188) – tripleee Mar 25 '19 at 16:34
30

You could do:
sh scriptname.sh

Shantha Kumara
  • 3,272
  • 4
  • 40
  • 52
xCra2yx
  • 577
  • 4
  • 5
  • 7
    Downvote: That's wrong if it's properly a Bash script. – tripleee Mar 05 '15 at 13:22
  • 1
    Upvote: This is fine on Mac OS X if your bash script is in the same directory – Karl Taylor Mar 04 '16 at 13:01
  • 31
    @kot "It happens to work for me" is very far from "this is a correct answer". It *can* work if `sh` is a symlink to `bash`, or if the script does not use any Bash-specific construct. In the former case, using `bash` instead of `sh` is the only correct, portable solution; in the latter case, it's not the correct answer to this particular question, because the OP asked about advice for a Bash script specifically. Perpetrating the wrong answer is irresponsible; users who fail to grasp the difference frequently post here, and need to be shown why this "worked for me" answer did not work for them. – tripleee Apr 03 '16 at 18:29
  • 1
    @kraftydevil too ^ One of several popular duplicate targets on this topic: http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash – tripleee Apr 03 '16 at 18:33
  • @tripleee Did not say 'It happens to work for me'. Maybe you should read the OP question again, How does he execute his bash script, and running the command that xCra2yx posted works was simply just adding my 2 cents :) – Karl Taylor Apr 03 '16 at 18:54
  • @tripleee I don't think it's a duplicate because it's asked with a specific reference to the platform (at least vs the question you posted) – kraftydevil Apr 03 '16 at 19:52
  • @kraftydevil Not a duplicate of this question, no, but a common duplicate for the follow-up question when `sh` does not work for some reason. – tripleee Apr 04 '16 at 04:55
  • 5
    @kot I repeat: There are situations where this works (just like you found out) but it is not a correct answer in the general case to the question in the headline. At the very least, this answer should explain the conditions under which it works; but my suggestion would be to simply delete this answer. – tripleee Apr 04 '16 at 04:59
  • Downvote: Ignores the shebang and uses the program corresponding to sh. As far as I know, this is in most linux distributions only a link to an arbitrary shell and also often not bash as explicitly specified in the shebang. E.g. in my Ubuntu 16.04 it is dash. To check run in terminal "ls -l /bin/sh" or "file /bin/sh". If not found run "ls -l $(which sh)" or "file $(which sh)". – guru Aug 26 '19 at 14:01
  • Downvote: not bash, and osx isn't linux and doesn't run bash either. – Software Engineer Jun 21 '20 at 18:41
  • Will a shebang overwrite this method of invocation? – BLang Sep 17 '21 at 18:50
19

Firstly you have to make it executable using: chmod +x name_of_your_file_script.

After you made it executable, you can run it using ./same_name_of_your_file_script

Alec
  • 160
  • 3
  • 11
Donald Shahini
  • 835
  • 9
  • 20
15

Change your directory to where script is located by using cd command

Then type

bash program-name.sh
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
gaurav07
  • 159
  • 1
  • 3
  • 7
    There is no need to `cd` anywhere; you can speciy an arbitrarily complex path name as the argument to `bash`; indeed, any program which takes a file name argument works this way (or is terrifyingly, mind-numbingly broken). – tripleee Apr 03 '16 at 18:24
5

And yet one more way

. /path/to/script

What is the meaning of the dot?

Eat at Joes
  • 4,937
  • 1
  • 40
  • 40
  • 1
    This executes the script in the current shell, which needs to be called out. This is not what most people expect to happen when you simply run a script. This can have side-effects on your existing environment. – Software Engineer Jun 21 '20 at 18:42
-2

If you are in a directory or folder where the script file is available then simply change the file permission in executable mode by doing

chmod +x your_filename.sh

After that you will run the script by using the following command.

$ sudo ./your_filename.sh

Above the "." represent the current directory. Note! If you are not in the directory where the bash script file is present then you change the directory where the file is located by using

cd Directory_name/write the complete path

command. Otherwise your script can not run.

GraniteSOS
  • 49
  • 2
  • 10
  • Hope fully all will do this because I mention all the related concept about this particular problem. – Hassan Shamshir Feb 01 '19 at 14:06
  • 1
    As explained in other answers here, `sudo` is *wrong* and potentially **horribly wrong** if you use it on an untrusted script which abuses the powers you needlessly gave it. – tripleee Mar 25 '19 at 16:00
  • The advice to `cd` somewhere is hard to understand, but probably also wrong. – tripleee Mar 25 '19 at 16:01