How to run .sh in Linux

Updated: 01/24/2018 by Computer Hope

In Linux, files that have the file name extension .sh are usually shell scripts, which are programs that are interpreted by the command shell. Shell scripts do not need to have a file name extension. In fact, they can be named anything at all. For historical reasons, however, they often have the extension .sh.

Making shell scripts executable

Shell scripts must be marked as executable before you can run them. You can check the permissions of a file, also known as its file mode, by viewing a long directory listing with the ls -l command. If you provide the file name, you will receive the information just for that file. For instance, here we have a file called somefile, and we view the permissions with the command ls -l somefile:

ls -l output

The ten characters "-rw-r--r--" represent the file mode of somefile. For more information about file permissions, see What are permissions, and how do they work?

Here, the file mode indicates that we can read and write somefile, but cannot execute it. We can set the execute permission using the chmod command, which changes the file mode:

ls -l output after chmod u+x

The command chmod u+x somefile sets the execute permission bit for the file's owner. You can see that the permission line now has an x in the fourth place, which represents the owner's execute permissions on the file. Also, notice that the file name is listed in color (here, it is shown in green). The color might be different on your system, but many Linux distributions are configured to indicate executable files using a distinct color when you run the ls command.

You can follow this same process for any .sh script file you may need to execute. For instance, if you write a custom script and name it, you should run chmod u+x before attempting to run it.

Executing a script from the command line

To execute a shell script, you need to specify the pathname of the script at the command prompt. For instance, if the script is named and it is located in your working directory, you can run it with the command below.


The above commands tells the shell, "execute the file that is located in the current directory." If is located in the directory myscripts in your home directory, you could run it with the command below.


Note: Specify the pathname to the script file (rather than just typing in, for instance) as a security measure: the shell wants to make sure it is running the correct, so it requires you to specify its exact location.

The only exception is if your script is located in a directory listed in your system's PATH environment variable. If that were the case, you could execute the file by running the command