How to handle errors in Bash

There are different techniques that we can use to handle errors in bash/shell scripting. Let us see each of these techniques one by one.

The easiest way to prevent errors is first to run your command in the terminal. And, if the command in the terminal runs as expected, you should add it to your script. Let’s look at the different ways you can debug and handle errors in Bash.

YouTube player
If you are new to Bash scripting, you may want to read the Bash Scripting Basics and Most Used Linux Commands post.

Using the Bash script’s exit status

Suppose you run the following commands in the terminal one by one:

pwd
echo $?

Here $? is a special variable that you can use to check the exit status of the last executed command. In this case, echo $? will return the exit status of the pwd command.

Remember, the exit status value can be zero ( 0 )  or greater than zero, where zero means success, and the value greater than zero means failure.

Now, let’s make a script and check the exit status of multiple commands.

#!/bin/bash
whoami
echo "exit status of whoami command: $?"
fdw
echo "exit status of fdw command: $?"
pwd
echo "exit status of pwd command: $?"
mand
echo "exit status of mand command: $?"

When you run this script, you will notice that only two commands return the exit status of zero. It means that these commands are executed successfully. In contrast, the other two commands, fdw, and mand fail to execute successfully.

How to stop any script on the first Error

Instead of getting all the script errors, you might want to exit it on the first error.

Let’s see how we can stop the script after it comes across the first error.

#!/bin/bash
set -e
whoami
fdw
pwd
mand

If you run this script, it will exit as soon as it encounters the first error. To stop the script at the first error, we have to use a command set -e at the top of the script, as you can see in this script.

This script contains two commands fdw and mand, that do not exist in Linux. So, the first error will occur when it tries to execute the fdw command.

Now, let’s see another script in which we use the command set -u instead of the set -e command at the top.

#!/bin/bash
set -u
echo "value of variable is $var"
pwd

Using set -u at the top of your script will exit your script if you use an uninitialized variable.

How to debug Errors in your Bash script

To debug any script, you can use the x option while running the script. Let’s make a script named script.sh and run it with this option.

#!/bin/bash
uname
whoami
hostname
pwd

Now, run this script with the following command:

bash -x script.sh

When you run any script with the x option, it shows each command of that script followed by its output.

It shows that command followed by the error of that command if there is an error. And, then you can go ahead and correct that error.

How to handle error through the if-else statement

Besides using the e and u option at the top of the script, we can also handle the errors using if-else statements. But, first, let us create a script that prints the file’s contents in case the file exists in the current directory.

#!/bin/bash
filename='info.txt'
for file in *
do
   if test -f "$filename"; then
       cat info.txt
       exit
   fi
done
echo "Error - "$filename" does NOT Exist"

This script searches for a file named info.txt in the current directory. Then, it prints the contents of this file through the cat command if the file exists; otherwise, it throws an error that the file does not exist.

How to handle errors using AND & OR

We can also use AND ( && ) and OR ( || ) to catch errors in a script or in the terminal. The syntax of using && is as follows:

First_command && Second_command

The second command will only work if the first command does not throw any error. If the first command throws an error, then you will not execute the second command.

Let us run commands using &&:

pwd && ls

In this case, both commands will be executed as the first command will not throw any error.

Similar to &&, the syntax of using || is as follows:

First_command || Second_command

In this case, either the first command will run or the second command will run. Both commands might run in this case. We will get the error only when both commands can’t run successfully.

So, in short, there are different ways through which we can catch and handle errors.

Need more info? Click here to view the official GNU Bash manual.

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.