Some Other Uses
Shebangs allow you to choose the interpreter which runs a script, so that the user does not have to select the interpreter at run time. This can be used to turn a script into a command, but it also has some other uses.We are going to go over some of them.
1. Ensuring Compatibility
Different distros have different shells, and sometimes users override their shells. For instance, some users prefer to use Zsh, the shell which comes with Apple’s MacOS. This can lead to some problems when for the people who write shell-scripts. For instance, if we write a script which behaves differently on Bash and Zsh, the script will run differently on the machines of people who use Zsh.
To avoid this, we can specify the shell we wish to run the script on. For instance, we can write the script to be run from The Bourne Shell (sh), which is the old UNIX shell. The Bourne Shell is present on Linux, MacOS and all other UNIX-based systems. Hence, we can ensure that our script runs identically on all of these systems by writing a script for the Bourne Shell using this shebang:
#!/bin/sh
2. Specifying Interpreter Version
There exist differences between different versions of the same shell. For instance, the Bourne Again SHell (Bash) has different versions. When you go through the list of CVE (Common Vulnerabilities and Exceptions) list associated with versions of Bash, you will find that security vulnerabilities exist in certain older versions of Bash which have been patched in later versions.
Unfortunately, enterprise system users (which are usually large corporate bodies) cannot simply upgrade their enterprise Linux systems. They need to pay money for licensed Linux systems from Red Hat, Oracle, SUSE, etc. So they use old Linux machines which have older versions of Bash. To avoid the vulnerabilities, they can install the latest version of Bash. But to set this version as the default for a shell script to run with, they override the default shell using a shebang. For instance, to ensure that the script runs with Bash 5.2, they can use:
#!/bin/bash-5.2
One possible downside is that, when a newer version of Bash is installed (say Bash 5.3), all the scripts used by the sysadmins and devops personnel will have to have their shebang line updated to Bash 5.3; to get around this problem, the sysadmin creates a symlink (which is essentially a shortcut to a file) to the latest version of Bash, and gives it a name like bash-latest. Then the shebang simply points to this symlink:
#!/bin/bash-latest
When a later version of Bash is installed, the symlink is deleted and a new one is created at the same location, with the same name, but pointing to the newer version of Bash.
3. Preventing Direct Execution
Sometimes you don’t want a shell-script file to be executed. Maybe you only want it to be invoked internally, using commands like source or using the os.popen function in Python. If you make a software with such a script, and you want to prevent a user from unexpectedly running the script, you can deliberately use a shebang which points to an executable that cannot interpret the script. A common choice of such an executable is false, which returns a non-zero value to the shell (to indicate an error), and does nothing.
To disable a script from being run in the shell by a user who didn’t read your README file, just use this shebang:
#!/usr/bin/false
You can still run a script with such a shebang using the source keyword, but it cannot be run directly as an executable.
Using Shebang in Linux
Imagine you’re working on a Linux computer and have a recipe with instructions written in different languages. But your computer only understands English! That’s where the shebang comes in. It’s like a tiny label at the beginning of the recipe that tells your computer which “translator” to use for each language. This magic symbol, #!
, followed by the interpreter’s path, is the key to understanding and running all sorts of scripts on your system.