Shebang is a special character sequence consisting of the hash and exclamation mark (#!
) at the beginning of the script.
This is used to tell the operating system which program is used to parse the rest of the script.
The bash shebang is also known as hashbang
, pound-bang
, hash-pling
, and sha-bang
.
In this article, we will discuss the usage of shebang in bash scripts along with some examples.
What is the Bash shebang Interpreter Directive?
In Bash, Perl, or Python scripts you probably have seen shebang i.e. #! followed by some paths such as the below examples:
#!/bin/bash
OR
#!/usr/bin/perl
OR
#!/usr/bin/python3
OR
#!/usr/bin/env bash
Have you ever thought why is this used? The Shebang on Bash or any shell script in Linux is used as an Interpreter Directive and it is not a comment, if you have ever thought so!
In general, the shebang interpreter directive takes the following form.
#!interpreter [arguments]
While using the shebang interpreter directive keep the following things in your mind.
- The directive should be the first line in the script
- It should always start with a shebang character i.e. #!
- The interpreter is a full path to an executable binary file for example
/bin/bash
- Interpreter arguments are optional
- You can use white space after shebang this is optional
Ignoring The shebang interpreter directive
If you do not specify the interpreter line in a script then the script will parse using the default interpreter set for your shell.
For example, suppose you want to parse a script using /bin/bash
then you should mention it as the shebang interpreter directive otherwise it will be parsed using the default program on most systems by default it is /bin/sh
.
Example of Using shebang in Bash Scripts
There are two ways to use the shebang interpreter directive to set the default program to parse the script.
1. Using the absolute path to bash binary
#!/bin/bash
2. Using env program
#!/usr/bin/env bash
You can use either way to mention the shebang interpreter directive. The benefit of using the second approach is that it will search for the bash
executable in the user’s $PATH
environmental variable. If it finds more than one path of bash
, the first one will be used by the script.
The given example shows a sample script.
#!/bin/bash
# bash script to add two numbers
echo "Enter first number"
read a
echo "Enter second number"
read b
((sum=a+b))
echo "The sum is $sum"
Before you execute this provide executable permission to this file. Let’s say you saved this file with the name ex1.sh
. Use the given command to make it executable.
chmod +x ex1.sh
Now you can run this script using –
./ex1.sh
This will display output something like the given in the image.
Alternatively, you execute this script using the given command.
bash ex1.sh
But this will override the interpreter set in the shebang line. Using this method can lead to unexpected behavior of the script so try to avoid this.
Conclusion
I hope now you understand what is shebang and how to use it in bash scripts. Now if you have any queries then write us in the comments below.