[bash] How do I execute a bash script in Terminal?

I have a bash script like:

#!/bin/bash

echo Hello world!

How do I execute this in Terminal?

This question is related to bash terminal

The answer is


And yet one more way

. /path/to/script

What is the meaning of the dot?


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


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.


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

Then type

bash program-name.sh

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?

Confusions and Conflicts:

  • You do not need an 'extension' (like .sh or .py or anything else), but it helps to keep track of things. It won't hurt. If the script name contains an extension, however, you must use it.
  • You do not need to be in any certain directory at all for any reason.
  • 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.
  • 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. Great post here.

(A person who is just learning how to execute scripts should not be using this command unless there is a real need, like installing a new program. A good place to put your scripts is in your ~/bin folder. You can get there by typing cd ~/bin or cd $HOME/bin from the terminal prompt. You will have full permissions in that folder.)

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 full path with the script name (e.g. /path/to/script.sh). You can verify the full path by typing pwd or echo $PWD in the terminal.
    • Execute from the same directory and use ./ for the path (e.g. ./script.sh). Easy.
    • Place the script in a directory that is on the system PATH and just type the name (e.g. script.sh). You can verify the system PATH by typing echo $PATH or echo -e ${PATH//:/\\n} if you want a neater list.
  2. Tell the system that the script has permission to execute. (pick one)

    • Set the "execute bit" by typing chmod +x /path/to/script.sh in the terminal.
    • You can also use chmod 755 /path/to/script.sh if you prefer numbers. There is a great discussion with a cool chart here.
  3. Tell the system the type of script. (pick one)

    • Type the name of the program before the script. (e.g. BASH /path/to/script.sh or PHP /path/to/script.php) If the script has an extension, such as .php or .py, it is part of the script name and you must include it.
    • 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.

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.


You could do:
sh scriptname.sh


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

bash /path/to/scriptname