Today I Learned: Linux script shebang

Brendan Thompson • 1 June 2021

Now, this TIL is also rather an old one, I would say this goes all the way back into the early days of my career in the IT industry. However, today I needed to look into shebang lines again which is why I thought it would be a good idea to write this little TIL.

The shebang line at the top of a script allows the script to automatically invoke the defined interpreter. In a lot of cases this is just bash or sh, however, it is absolutely not limited to this. Some other commons ones that are used are python, swift, perl.

For the sake of ease I will use bash in any examples, however all examples should be applicable to other interpreters.

The following is the most basic example, wherein you define the explicit path to where bash is.


echo "Hello, World"

This is pretty standard practice, however it does mean that the script is not super portable. If you were to try an execute this script on another machine where bash was installed to /usr/bin/ instead of /bin/ the script would fail to run. A way to fix this would be using env to have the script automatically find bash.

#!/usr/bin/env bash

echo "Hello, World"

Now that the shebang is using env the script can execute on any system where bash is installed.

In the specific case of bash the operator is able to append -- to the end of the shebang which ensures that no additional options that might be passed in will be processed.

#!/usr/bin/env bash --

echo "Hello, World"