There are three ways to run a shell script on a Linux system:
- Specifying the script filename as an argument to the bash command
- Executing the script by specifying its path
- Using the Desktop Environment File Manager (ex: Nautilus)
I am going to use the following “script.sh” file to show you the examples.
$ cat script.sh echo "Hello, It's Linux TLDR"
Method 1: Specifying the Script Filename as an Argument to the Bash Command
Note: Shell is just a program, and Bash is the implementation (or superset) of that, providing you with more features out of the box.
In this method, you will pass the script filename as an argument to the bash command.
$ bash script.sh
The advantage of using this method is that you do not need executable permission for your script.
$ ls -l script.sh
Ensure the script you are passing as an argument to the bash command contains only the syntax and commands supported by bash.
Also Read: What is Exit Status Code ($?) of Last Command in Linux
If you try to execute an invalid string or command, you will get the following error:
You can use this method without any issues unless your system provides bash as the default interpreter.
Execute the following command to find out your system’s default shell interpreter.
$ echo $SHELL #OR $ echo $0
Note that there are other shells also available, like ZSH and Fish, that can be used to execute your shell script.
If your system does not provide bash as the default interpreter and your script is not compatible with other shells, then most of the time the system provides bash as an alternative shell.
Execute the following command to list all the existing shell interpreters in your Linux system.
$ cat /etc/shells
Method 2: Executing the Script by Specifying its Path
In this method, you need to specify the absolute or relative path pointing towards your shell script with executable permissions.
If you try to execute your script without executable permission, then it will throw the following error:
To avoid the above error, give your script executable permission using the chmod command:
$ chmod +x script.sh $ ls -l script.sh
Execute your shell script by specifying its path after assigning the executable permission.
The ./ or Full Path is Required to Execute Your Shell Script
Ensure to always specify “./” at the beginning of the shell script name when you are in the same directory as the script.
If you are somewhere else, specify the full path pointing towards your shell script, as shown.
What if you don’t use the “./” or don’t specify the full path of your shell script and try to execute it with its file name? Let’s see:
As you can see, when you don’t specify them, your shell program will look for the specified file in the directories specified in your $PATH environment variable.
Execute the following command to get the list of directories specified in your $PATH variable.
$ echo $PATH
These directories store binary programs like ls or echo, which you can execute from anywhere.
However, your shell script does not reside in those directories and is also not a binary file, which is why you got the “command not found” error.
What is Shebang or #! /bin/bash at the Beginning of the Shell Script?
The “#! /bin/bash” (also known as “shebang“) is used to notify your current shell which interpreter you want to use to execute the specified shell script.
As I’ve told you earlier, the shell is a program, and bash is the implementation of it. Apart from bash, there are other shell programs available, like ZSH and Fish.
Most of the time, you will use the shell or bash as the shebang because other shell programs are not defaults in most Linux systems, which will give you the following error:
Does the shebang matter? If you don’t specify the shebang and run your shell script using its path name (ex: “./script.sh“), your shell script will be executed by the default shell interpreter in your system.
So shell script writers do not specify the shebang at the beginning of the shell script? Look, most of the shell interpreter shares a common syntax, but some might differ.
For example, in ZSH, the array index starts at 1, and in bash, it starts at 0.
So, it will be good practice if the shebang is specified at the beginning of the shell script to avoid any unusual errors.
Method 3: Using the Desktop Environment File Manager (ex: Nautilus)
If you are using an Ubuntu system, you can use your file manager to run the shell script. In this case, the default shell interpreter will be used to execute the shell script.
The steps to run shell script using the file manager are followed by:
- Open Nautilus file manager.
- Navigate into the shell script directory.
- Right click on the shell script and open the property window.
- Go to the Permissions tab and give executable permissions to the owner.
- Close the property window.
- Right click on the shell script.
- Click on the “Run as a program” button.
And that was the end of the article.
If you have any suggestions to include in this article, feel free to tell us in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.