Preventing Bash Script Disasters

Kanishk Dudeja
Oct 5, 2016 · 2 min read

So you just wrote a bash script and you’re scared of running it on your system due to a lurking rm -rf statement in the script. Here is some stuff you can use to prevent disasters due to unhandled errors in your scripts:

1) Use set -u (Just put set -u at the top of the script):

In this mode, the script will exit if we try to use an uninitialised variable. Useful for preventing rm -rf $uninitialized_variable/ type disasters

This translates to rm -rf / if this mode is not on and will destroy your whole system.

2) Use set -o pipefail:

This mode causes a pipeline to produce a failure return code if any command results in an error. Normally, pipelines only return a failure if the last command results in an error.

This can be useful in preventing issues which occur due to an error which occurs in the non-last command in the pipeline.

3) Use set -e:

In this mode, any command our script runs which returns a non-zero exit code will cause our script to itself terminate immediately with an error. Basically script won’t continue if an error occurs.

4) The above mode is fine but if an error is detected, the script basically halts. Therefore, you won’t to be notified that an error occurred in your script. So, I basically comment(not use set -e) this mode and use some manual error checking after every command.

function checkStatusOfLastCommand {
if [ “$1” -ne 0 ]
mail -s “Something Failed” <<< “$2”

checkStatusOfLastCommand $? “COMMAND_1_FAILED”
checkStatusOfLastCommand $? “COMMAND_2_FAILED”
checkStatusOfLastCommand $? “COMMAND_3_FAILED”
checkStatusOfLastCommand $? “COMMAND_4_FAILED”

$? contains the exit code of the last executed command. If it is not 0, then it means some error occurred. So I pass this exit code as well as a tag describing the previous command to a function which checks if the return code is not equal to 0, and if yes, then e-mails us the command which failed.

So as soon as I detect that an error occurred, I terminate the script with the ‘exit’ statement then and there. Because error cascading can produce further errors and might do some irreparable harm.

Feel free to share more tips on how can bash script disasters be prevented.

Cross-posted on:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store