Glossary

Shebang

The shebang (or hashbang) is the two-character sequence #! at the very start of an executable script file, followed by the path to an interpreter. When the kernel sees a file beginning with #!, it invokes the specified interpreter with the script file as its argument, enabling scripts in any language to be run as ordinary commands.

#!/bin/sh
#!/bin/bash
#!/usr/bin/env bash
#!/usr/bin/env python3
#!/usr/bin/perl -w
#!/usr/bin/awk -f

The #!/usr/bin/env bash form is preferred for portability: env looks up bash in $PATH, so the script works whether bash is at /bin/bash, /usr/bin/bash, or /usr/local/bin/bash. The older #!/bin/bash works on most Linux systems but fails on BSD and some minimal images.

The comment convention (# being the shell's comment marker) is elegant: the shebang line is both a directive to the kernel and a valid comment in the script's language. The name "shebang" is a portmanteau of "sharp" (#) and "bang" (!). Without a shebang, a script can only be run by explicitly invoking an interpreter (bash myscript.sh).

Related terms: Shell Script, bash, exec

Discussed in:

Also defined in: Textbook of Linux