Force A Bash Script To Exit On Error

Bash scripts are a useful tool for anyone that works with the Linux operating system. Bash scripts are useful to automate tasks that are repetitive or complicated. The more a script does the more likely it will encounter errors during execution. In most higher level languages when a error is hit the script will immediately stop execution. This is not the case by default in bash scripts unfortunately.

Consider the following contrived example. You have a script that will delete a file named important.file that is located in a directory called garbage.

Assume the directory structure looks like this.


test$ tree
.
├── delete_file.sh
└── important.file
0 directories, 2 files

The delete_file.sh is the script with the code above, and note that the garbage directory does not exist. What will happen if the script is executed?

test$ ./delete_file.sh 
./delete_file.sh: line 6: cd: garbage: No such file or directory
test$

We got an error saying that there is No such directory, but what about the important.file. The script wants to delete the important.file in the directory garbage.

test$ tree
.
└── delete_file.sh
0 directories, 1 file

Oh No! The script deleted the important.file that I wanted to keep!

What the script writer wanted to happen was if the garbage directory was not there they did not want to execute the rm important.file command.

To make the bash script stop immediately if there is an error we need to use the set -o errexit option in the bash script. So now our contrived example script will look like this.

Now when the script is executed this is what it will look like.


test$ ./delete_file.sh
./delete_file.sh: line 5: cd: garbage: No such file or directory

We got the same error as before, but did it delete the important.file?

test$ tree
.
├── delete_file.sh
└── important.file
0 directories, 2 files

The important.file is still there so the script did not continue executing and stopped when it got the error trying to cd to a directory that did not exist.

This feature is important to use if your bash script is doing destructive actions. You do not want the script to continue executing if an important part of the script fails where the destructive action assumes that the commands before it had executed successfully. With this option enabled you may discover issues in your script that you may not even know exist so it will raise the script quality. It will also make scripts easier to debug because it will fail at the line of the error rather than later in script.

If you use command pipes in your script read this article to learn how to detect errors in them. If you found this article useful let me know by giving me a clap.

--

--

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
Zack Fredenburg

Zack Fredenburg

Software Engineer with extensive experience in testing, test automation and development.