Start with a shebang

Start with a shebang#

Aim of this section: Make the script runnable as long as Bash in is the $PATH.

The first line of a script is special. If it starts with the characters #! the rest of that line is used as the path to the interpreter for the script when run directly. This line is usually called the shebang, from the “hash” and “bang” characters.

On some systems the path to the Bash interpreter command is /bin/bash, on others it’s /usr/bin/bash. Part of the freedom of a Linux system is that you can literally install anything anywhere, but these are by far and wide the two most common options. In short, this is not standardized, so there’s no single Bash path we can use in a shebang directly. Fortunately there’s a tool called env which can be used to find the path of an executable on your $PATH and run that. env has a much more reliable path, so this is a portable shebang line:

 

This page is a preview of The newline Guide to Bash Scripting

No discussions yet