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.

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:

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.

echo "exit status of whoami command: $?"
echo "exit status of fdw command: $?"
echo "exit status of pwd command: $?"
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.

set -e

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.

set -u
echo "value of variable is $var"

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 and run it with this option.


Now, run this script with the following command:

bash -x

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.

for file in *
   if test -f "$filename"; then
       cat info.txt
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 the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

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