Shell scripting is a text file that contains a list of commands which can be executed in the Linux system using a shell as a command line interpreter. On a very high level, there are three basic components of shell scripting Shebang, Commands, and Shell Construct. The shebang is the sequence of “#(hash)” and the “!(exclamation mark)” characters that follows the absolute path of the interpreter.
This guide explains the “Bash Shebang” in detail. The content of this guide is written below:
Let’s start with the basics of “Shebang”.
What is Shebang?
The shebang defines the interpreter program that will be used during the execution of the file. Shebang is also called the “sha-bang”, “hashbang”, “pound-bang”, and “hash-pling”.
The basic syntax of the Shebang line is typed here:
#! <interpreter> [arguments]
In the syntax, the arguments are optional.
What is bash Shebang in Linux?
The “bash” shebang is the first line of the Linux bash script that executes the commands written in it. No matter what the default shell is, the script is always run in the bash script. The one thing that needs to be remembered is always mentioning the full path of the binary file, such as “/bin/bash”.
The generalized syntax of the “bash” shebang is written below:
Note: If the user does not specify an interpreter line, then the default interpreter line is “/bin/sh”. Hence it is suggested to define “#!/bin/bash” for the execution of bash scripts.
How to Implement Bash Shebang in Scripts?
The bash shebang can be easily applied to any shell script in a Linux system. For this, open the script using any text/code editor. In our case, we use “nano”, and the script name is “script.sh”
$ nano script.sh
Now, add the shebang, i.e., “#! /bin/bash” as shown in the following screenshot:
The first line displays the “bash shebang” line while the other two define the “echo” commands.
Make the “script.sh” script executable by changing its permissions using the “chmod” command. After that, execute the script by utilizing the following commands.
$ chmod +x script.sh #to make the script executable. $ ./script.sh #to execute the script.
Note: The users can use the following command to check for the availability of shells.
$ cat /etc/shells
The above command displays the available shell in the current Linux distribution “Ubuntu 22.04”
That’s all about the “bash shebang” in Linux.
In Linux, the “bash” shebang is the first-line interpreter used to execute the commands written in the “bash” scripts. It starts with the combination of “#!” signs which follow the absolute path of the interpreter. If no interpreter is defined, then the script executes in the “/bin/sh” by default. This guide has provided a detailed view of the “bash” shebang in the Linux/Unix operating system.