Shebang signalling

2023-11-04
2 min read

Background of shebang

#!/bin/bash

The #! notation at the beginning of a script is indeed often referred to as a “shebang” or “hashbang” derived from the combination of character names used in the #!. It’s a special code that indicates to the operating system how the script should be executed. This construct provides a way to signal the operating system that what follows is the path to the interpreter

Here’s a breakdown:

Hash: The # character is commonly referred to as a “hash” in British English. In American English, it’s often called a “pound sign,” but with the rise of hashtags in social media, the term “hash” has become more universally recognized. In the context of programming and scripting, it’s generally referred to as a “hash.”

Bang: The ! character is colloquially called a “bang” in programming and computing contexts. This name has its origins in comic books, where the term “bang” would be used in a caption or sound effect to represent a loud noise or gunshot.

Combining these names

“Hash” + “Bang” = “Hashbang” “She” (from the ‘sh’ in “hash”) + “Bang” = “Shebang”

Both “hashbang” and “shebang” are colloquial terms, and their use is a bit of informal lingo that has been adopted by programmers and sysadmins.

/bin/bash: This is the path to the interpreter that will be used to execute the script. In this case, it’s pointing to the Bash shell, which is commonly located in /bin/bash. So, when you have #!/bin/bash at the top of your script and you execute the script, the operating system understands that it should use the Bash shell to interpret and run the script.

In essence, the shebang provides a level of abstraction. Instead of always typing bash myscript.sh, you can make myscript.sh executable and simply run ./myscript.sh, and the operating system will know to use Bash to interpret it, thanks to the shebang.


A few things to note:

  • The shebang must be the very first thing in the file. Even a space or a newline before it will cause it not to be recognized. You might encounter different shebangs depending on the script’s intended interpreter. For instance:

    • #!/usr/bin/env python3 would be used to specify that a script should be run with Python 3, by finding it in the system’s PATH

    • #!/usr/bin/perl would indicate the script is a Perl script

  • The shebang is not strictly necessary for all scripts. However, it becomes essential if you want to make the script executable (with chmod +x) and then run it directly without explicitly calling the interpreter.