Make your shell scripts safer with set builtin.

brianbola profile image Brian McNabola ・1 min read

set allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables.

set -e
The -e option will cause a bash script to exit immediately when a command fails. This is generally good as it prevents damage being caused by unexpected errors.

set -u
This option causes the bash shell to treat unset variables as an error and exit immediately. This is useful for debugging & catching typos in varible names.
Also for keeping scripts cleaner.

set -o pipefail
This particular option sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or to zero if all commands of the pipeline exit successfully.

This is extremely useful for stopping a script execution causing damage when the previous command in the pipe was unsuccesful.

Include the below in your next script and reap the benefits.

set -euo pipefail


Editor guide
msoedov profile image
Alex Miasoiedov

set -x for troubleshooting