Bash Script Shebang

As we have discussed in the previous article that every bash script should start with the following line:
Here, the sequence of characters (#!) is known as “Shebang”. It is used to communicate with the operating system so that it can decide which interpreter should be used to parse the rest of the file.

Shebang Interpreter Directive

The shebang interpreter directive in bash script takes the following form:
#! Interpreter [arguments]
There are some important points to remember before we start explaining how to use shebang, which are mentioned below:

The directive must be used in the first line of the script.
The directive must start with the shebang (#!).
White space after the shebang characters is optional, not mandatory.
An interpreter is a full path to a binary file. (i.e., /bin/bash, /bin/sh).
Interpreter arguments are optional.

For example, #!/bin/bash uses ‘bash’ to parse the file.

How to use Shebang in Bash Scripts?

If you do not specify the shebang and try to run the Bash Script using another shell, then the script will be parsed by the default interpreter used by that specific shell. For example, ‘bash’ uses ‘bash’ and ‘zsh’ uses ‘sh’ as a default interpreter. To ensure that your script is always interpreted with the Bash, you have to specify the executable path with the help of shebang.

There are two common ways that you can use to specify the shebang directive and set the interpreter.

Method 1: 

You can use the absolute path to the bash binary, i.e.,

Method 2: 

You can use the ‘env’ utility, i.e.,
#!/usr/bin/env bash
The advantage of using this method is that it will look for the bash executable in the user’s $PATH environmental variable. In case there is more than one path to bash, then the first one will be applied by the script.

Bash Script Example

First of all, create an empty script file by using the following command:
$ touch
Open this file in a text editor and paste the following codes:
echo “Hello, User.”
To run this script, you need to make it executable using the following command:
$ chmod +x
Now, you can run the script by using ./ followed by the script name, i.e.,
$ ./


Hello, User.

How to override the Shebang?

If you want to override the interpreter in the Shebang line, you can run the script explicitly by specifying the required shell.

For example, if you want to run the script that includes #!/bin/bash in the Shebang line, you would use the command as given below:
$ bash hello_user
However, it is recommended not to use this method as it may lead to unexpected behavior of the script.

←prev next→

Tutorial Highlights

Weekly Hits

Latest Tutorial

© 2024 TutorialsMate. Designed by TutorialsMate