Good Practices in bash scripts
Shebang (#!)
It is called a shebang or a “bang” line. It is nothing but the absolute path to the Bash interpreter. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. All scripts under Linux execute using the interpreter specified on a first line. This ensures that the correct interpreter will be used to interpret the script, even if it is executed under another shell.
set -e
this will make the shell script exit as soon as any line in the bash script fails. for example, a shell file like below will execute every line
After adding set -e, it will stop executing after the line that fails, in this case the one that returns false.
Ignore failure in scripts
if we don’t want the script to fail after certain failing statements, we can append these certain statements with || true.
set -x
this will make the shell print each line before execution. Combining this with previous set statement and same example, it will look like
set -u
The following example has variable b which is not set. The run of the script will be successful.
But adding -u to the same script will force bash to treat unset variables as an error and exit immediately.
set -o pipefail
bash usually looks at the exit code of the last command in a pipeline. This can cause a problem for -e option as it will only consider the leftmost command’s exit code in a pipeline. This particular option sets the exit code of pipeline commands to that of the rightmost command to exit with a non-zero status or 0 if all exit successfully.
the echo $? is a special variable in bash that shows the exit code of last run command.