A template for robust Bash scripts.
Open https://www.bash-template.com, then copy and paste.
wget -O - bash-template.com -o /dev/null
http -b GET bash-template.com
curl bash-template.com
With POSIXLY_CORRECT=1
we ensure that set
executes the Bash builtin set
and not some other shell function. This is because in POSIX mode, "builtins are found before shell functions" ([3], point 15). After set
commands, we return to default non-POSIX mode with unset POSIXLY_CORRECT
.
With set -o errexit
the script will exit immediately with non-zero return code upon error [1].
With set -o nounset
referencing unset variables will result in an error [1]. For example if no arguments were provided to a script, then $1
is not set and referencing it will result in error.
With set -o pipefail
a pipeline of commands will succeed (return code zero) only if all commands in the pipeline succeeded [1], not just the last one.
[1] https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html#The-Set-Builtin
[2] https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins
[3] https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html#Bash-POSIX-Mode