If you're a programmer, you might know what a hashbang (also known as a shebang) is – a shellscript might, for instance, start with the hashbang #!/bin/sh on the first line, which includes an absolute path to the sh interpreter. But what do hashbangs do?

Hashbangs - how do they work?

In effect, hashbangs act as interpreter directives on unix systems, which means that when a script containing a hashbang is run as a program, the interpreter specified in the hashbang is run instead, and the path to the script being run is passed as an argument to the specified interpreter.

As the hashbang is usually ignored by the language interpreters themselves, they enable users to run something like ./path/to/script.py and have the script executed as a Python program automatically - provided it is executable and contains a hashbang that specifies a Python interpreter.

This means that when we run the last command, what is really being run is something roughly equivalent to python path/to/script.py.


Not every system has the same file system layout, though. For instance, Python might be installed in /usr/bin/python, /usr/local/bin/python, /home/username/bin/python, or possibly in some other location. Though there are some conventions (e.g. FHS) in the world of Unices and Unix-like systems with regards to directory structure, POSIX does not standardize these locations (it standardizes very little). Because of this, a more portable way of specifying an interpreter might be needed.

One solution with regard to portability is to look up the path to the wanted interpreter via env.

This can be done by replacing the hashbang of this type:


with one of this type:

#!/usr/bin/env python

This solution works by the env utility invoking the first python it finds in the user's $PATH.

However, it might introduce problems for some distros and systems with "exotic" configurations. For more info about this, see this Wikipedia article.

Newer post
Setting Up a Computer for Deep Learning
Older post