By default, a Bash script on Linux will report an error but keep running.

Error Handling in Scripts

Handling errors is part of programming.

Even if you write flawless code, you might still run into error conditions.

Linux laptop showing a bash prompt

fatmawati achmad zaenuri/Shutterstock.com

The default action of the Bash shell is to print an fault signal and continue to execute the script.

This is a dangerous default.

How disastrous that turns out to be, depends on what your script is trying to do.

Making a script executable by using chmod

If the script has encountered a problem from which it cannot recover, it can shut down.

This is calledtheir exit status.

In Bash, zero equates to true.

Checking the exit status of a command to determine whether there has been an error

Copy this script into an editor, and save it to a file called “bad_command.sh.”

bad_command ); then

echo “bad_command flagged an error.”

exit 1

fi

You’ll need to make the script executable with thechmodcommand.

Using the logfical OR operator to call the error handler in a script

Substitute the name of the appropriate script in each case.

When we fire off the script we see the expected fault signal.

It cannot be executed, so the response is not zero.

Using the set command in a script to terminate the script if an error occurs

It might look like theexit 1line is redundant.

After all, there’s nothing else in the script and it is going to terminate anyway.

But using theexitcommand allows us to pass an exit status back to the shell.

Using trap in a script to catch Ctrl+c

This works because either the first command runsORthe second.

The left-most command is run first.

If it succeeds the second command is not executed.

Using trap with ERR to catch errors in a script

But if the first command fails, the second command is executed.

So we can structure code like this.

This is “logical-or./sh.”

This is held in the variable$1.

The function terminates the script with an exit status of one.

This is useful information when you’re debugging a script.

The exit status of the script is one.

The 127 exit status reported byerror_handlermeans “command not found.”

To do this, usethesetcommandwith the-e(error) option.

This tells the script to exit whenever a command fails or returns an exit code greater than zero.

Also, using the-Eoption ensures the error detection and trapping works in shell functions.

To see to it that errors are detected in piped sequences, add the-o pipefailoption.

A failing command in the middle of the piped sequence would not be detected.

The-o pipefailoption must come in the list of options.

set -Eeou pipefail

echo “$unset_variable”

echo “Do we see this line?”

When we launch the script the unset_variable is recognized as an uninitialized variable and the script is terminated.

The secondechocommand is never executed.

Typically this is used to catch signals such asSIGINTwhich is raised when you press the Ctrl+C key combination.

This script is “sigint.sh.”

It will be triggered whenSIGINTis raised.

The rest of the script is a simple loop.

We can usetrapwith theERRsignal to catch errors as they occur.

These can then be fed to a command or function.

This is “trap.sh.”

We’re sending error notifications to a function callederror_handler.

It passes the exit status from the failed command and the line number to theerror_handlerfunction.

Ourerror_handlerfunction simply lists the details of the error to the terminal window.

If you wanted, you could add anexitcommand to the function to have the script terminate.

Or you could use a series ofif/elif/fistatements to perform different actions for different errors.

It might be possible to remedy some errors, others might require the script to halt.

That’s in addition to making sure the execution flow and internal logic of your script are correct.

It shows each command with its arguments—if it has any.

This happens after the commands have been expanded but before they are executed.

It can be a tremendous help in tracking down elusivebugs.

Related:How to Validate the Syntax of a Linux Bash Script Before Running It