Bash Shebang Tutorial

Bash provides the Shebang characters in order to set the type of the script. The shebang consists of two characters which are # and ! . Simply the shebang is #! . The shebang is used at the start of the script file in the first line as the first two characters. This line specifies the interpreter’s name or path after the shebang characters.

Shebang Syntax

The shebang has the following syntax where it is located at the start of the file as we can call it the first 2 characters of the script file.

#!INTERPRETER

...
  • INTERPRETER is the interpreter executable path. For example, it can be /bin/bash in order to set the file as a bash script file.

Bash Shebang

Let’s make an example to set the scripting file language as bash. The bash default interpreter is set as /bin/bash .

#!/bin/bash

echo "This is bash script."

Alternatively, the script language can be set as bash by using the /usr/bin/env command like below.

#!/usr/bin/env bash

echo "This is bash script."

Override Shebang

Scripts generally contain the first line as a shebang and the script interpreter. This configuration shebang can be overridden by calling the script with a new interpreter. In the following example, we set the script interpreter as bash. But using the zsh interpreter, we override the shebang.

#!/bin/bash

echo "This is bash script."

We override the shebang with the zsh interpreter.

$ zsh myscript.sh

Shebang with Bash, Perl, Python, Python3 Interpreters

In the following table we list shebang usage with popular scripting languages and versions.

ShebangScript Language
#!/bin/bashBash Script
#!/usr/bin/env bashBash Script
#!/usr/bin/pythonPython Script
#!/usr/bin/python3Python3 Script
#!/usr/bin/perlPerl Script

Leave a Comment