How do I execute a bash script in Terminal?

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

bash /path/to/scriptname 
answered Feb 1, 2010 at 17:30 Dennis Williamson Dennis Williamson 357k 93 93 gold badges 379 379 silver badges 442 442 bronze badges

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 ( /(your conda installation path)/bin/conda init zsh ) and then close and reopen the shell one more time

Commented Jul 20, 2022 at 16:43 Excuse my ignorance but what's the difference between using and not using bash before the path? Commented Apr 4 at 10:54

@AlexD: If the script is not both marked as executable (e.g. chmod +x ) and has a shebang (e.g. the first line is similar to #!/bin/bash ) then the interpreter needs to be specified. Thus bash (or another interpreter) is used.

Commented Apr 4 at 12:57

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

50.7k 19 19 gold badges 125 125 silver badges 125 125 bronze badges answered Feb 1, 2010 at 15:53 prodigitalson prodigitalson 60.3k 10 10 gold badges 103 103 silver badges 115 115 bronze badges

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'

Commented Dec 28, 2019 at 10:59

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

./scriptname.sh 

if in the same directory or

scriptname.sh 

if it's in the bin folder.

answered Feb 1, 2010 at 15:52 John Boker John Boker 83.4k 17 17 gold badges 100 100 silver badges 130 130 bronze badges This will only work if the script has the execute bit set. That probably needs to be addressed. Commented Feb 1, 2010 at 16:15

./scriptname.sh works for me but scriptname.sh gives scriptname.sh: command not found . -rwxr-xr-x are its permissions.

Commented Apr 21, 2017 at 12:24

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.

Commented Mar 25, 2019 at 15:55

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

Extensions

Changing directories

Typing the program name

SUDO

Script location .

# 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 $ # or > printf "%b" "$" /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)

> bash /path/to/script.sh . > php /path/to/script.php . > python3 /path/to/script.py . 

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