Shebang #! Tutorial In Linux Bash

In computing and Linux the shebang is a character sequence that consists of # and ! . Simply the shebang is expressed as “#!”. The shebang is also called sha-bang, hashbang, pound-bang, and hash-pling. The shebang is used to specify an interpreter for script files. Linux is popular with its script files and scripting languages like Bash, Python, Perl, PHP, etc. The shebang is used to specify the interpreter type and location. When a script file is called the first line contains the shebang with the interpreter path.

Shebang Syntax

The shebang has the following syntax where the interpreter executable file path is appended after the shebang.

#!INTERPRETER
  • INTERPRETER is the interpreter path like “/etc/bash” etc.

Bash Shebang

The most popular shebang usage is the Bash Shebang. The Bash shebang is used for the bash scripts.

#!/bin/bash

Python Shebang

Python is very popular scripting and programming language that is used in Linux and Windows operating systems. The Python scripts also used the shebang in order to run Python scripts directly. The python interpreter is located under the “/bin/” directory with the name of “python”.

#!/bin/python3

PHP Shebang

PHP is another popular scripting and programming language. The PHP scripts can be also executed with shebang.

#!/bin/php

Perl Shebang

Perl is an old-school programming and scripting language where it was very popular 2 decades ago. The Perl scripts can be used with shebang in order to execute Perl scripts directly.

#!/bin/perl

Make Script File Executable To Run Shebang Interpreter

Using shebang to run a script is not enough. Also, the script file should be executable. The script file can be set as executable with the chmod command. In the following example, we set the bash script file named “backup.sh” executable.

$ chmod u+x backup.sh

Run Script Files without Shebang

Even shebang is used to run script files directly by calling them with their names it is not a must. Script files can be also executed without adding a shebang. The interpreter executable or path should be provided before the script file. In the following example, we run the “backup.sh” bash script file without a shebang.

$ bash backup.sh

Leave a Comment