how to run a script (.sh) file in linux from command line

Scripts are an integral part of any Linux or Unix systems. It refers to a piece of non-compiled programming code that requires an interpreter at run time to perform a task. The scripts can use any of the interpreted language such as Linux shell, Perl, Python, Ruby, JavaScript or any of the other hundreds of interpreted computer languages.

In linux shell environment, the script file usually have the .sh extension and is also referred to as the sh files. Sometimes, the files have extension specific to the shell that it is intended to run in, such as .bash, .csh or .ksh.

Scripts or Script files are used for a variety of purposes in Linux. Some Linux commands themselves are coded as scripts where as many of the compiled commands have several wrappers written around them to provide more convenience features. In addition, you can code your own custom script to automate many small tasks that needs to performed routinely. Also, many of the software have installer scripts that need to be run manually to install and configure products.

Shell scripts are Linux/Unix specific scripts that are designed and coded to run exclusively using the shell scripting languages available in Linux, such as sh, bash, csh, ksh etc. We will look at how to run these scripts, exclusively shell scripts (.sh files) from the command line.

In order to run the script file, you will need the appropriate permissions to execute it. If the execution bit is not set, then you should change the permissions on the script files using the chmod command.

chmod +x /path/to/scriptfile

The script files by convention have a .sh extension, but it is not necessary to have .sh extension or any extension for that matter. You can make any file (with any or no extension) executable by changing the permissions, and as long as the file content has the appropriate code that can be interpreted and executed by the shell, it should work just fine.

Run Script from Command Line

You can run the script by just typing in the name of the script, but that effectively depends on the PATH environment variable that set on the command shell. In order to make it independent of the variable settings, you can provide the path to the script file. You can provide either the relative path or the absolute path.

The generic syntax looks like this….

bash$ /path/to/scriptfile

An example of relative path

bash$ ./myscript.sh

and absolute path…

bash$ /home/tom/bin/myscript.sh

As mentioned, if the directory where the script file resides is in the PATH variable, then it is not necessary to provide the path to the file. You can just type in the script name and the shell will search all the directories in the path in order and execute the first one it finds.

A shell script can be executed in or using any of the supported shell environment in Linux. In a properly coded script, the first line or the shebang specifies exactly which shell to be used while executing the file. A Shebang or Shabang is usually the first line that looks something like…

#!/bin/bash

The command line shell will fork a process/sub-shell with the specified value and execute the script in the sub-shell. This helps to keep the current shell clean. This is the ideal way to execute the script in most cases, such as when you are installing a product or executing a command.

But many times, you would want the script to execute in the command line shell without forking a sub-shell. This is especially useful, when the script modifies some environment values (such as path) that you want to continue to use even after the script exists.

Run Script using the Current Shell

In order to run the script in the current shell without forking another process you can use either the . operator (dot) or the source command.

bash$ source ./myscript.sh

bash$ . /path/to/myscript.sh

They are mostly equivalent for most practical purposes, except for some minor differences depending on the shell you are using. The . (dot) operator mostly works in bash and its related shells while the source command will work in other shells as well, such as csh.

Another advantage when executing the script this way, either using source or the dot operator is that you don’t need the executable permissions for the file, just the read permission will suffice. This can be quite handy if you cannot modify the permissions for file for some reason.

Run Script using Another Shell

Sometimes, you will want to run the script using another shell rather than the one you are using in the command window. This is also one way to override the value that is set in the shebang of the script file.

You type the shell you want to execute followed by the script file name to execute the script. For example, to run the script named myscript.sh using the sh shell, you will use the following command

bash$ sh ./myscript.sh

or to use bash or ksh, just type

bash$ bash /path/to/myscript.sh

bash$ ksh /path/to/myscript.sh

Common mistakes and Troubleshooting

If you still cannot get a script to execute correctly, then you will need to troubleshoot. Most often, the error message should provide you with enough information to figure it out.

Check Permissions: If the script file or the user does not enough permissions to execute the file, the error message should be informative enough. You will see something like

bash: ./test.sh: Permission denied

You can modify the permissions of the script to give yourself the rights and you are good to go.

Check Path: The PATH variable is another cause for confusion especially if you are trying execute the script with out the full path. You can find the current PATH value by using the following command…

bash$ echo $PATH

You should make sure that the directory that the script file resides in is in the path. Also, make sure that there is not another script with the same name in another directory that is getting picked up first. The shell executes the first script it finds in the path.

If there happens to be another script or command with the same name, then you have three options

  • You can change name of the script to something else that is unique
  • You can modify the path and change the order of directories in the PATH variable, so that your script gets picked first
  • You can execute the script with the complete path, either relative or absolute