Catch and handle errors in Bash

Bash does not natively support a try/catch syntax for errors; however, there are options for handling errors within a script.

Try and Catch errors in Bash

For the first technique, simply detect if there’s a non-zero status by using the following syntax:

1
2
3
if ! command_call; then
    echo "command_call did not complete successfully"
fi

In addition, an || (or) expression may be used instead:

1
command_call || echo "command_call did not complete successfully"

For explicitness, the full syntax for a try/catch in Bash is:

1
2
3
4
5
if command_call ; then # try
    echo "tried and was successful"
else # catch
    echo "command_call did not complete successfully"
fi

If command_call is not successful, the echo statement will be invoked in all scenarios.

Error handling in Bash

For the second technique, bash provides a native variable for reporting exit codes: $?. Some users may want to catch and handle specific exit codes.

1
2
3
4
5
6
7
8
command_call

status=$?
if [ $status -eq 1 ]; then
    echo "General exception"
elif [ $status -eq 127 ]; then
    echo "The command could not be found!"
fi