| by Arround The Web | No comments

Making a Bash Script Return with Different Return Codes on Exit

Exit codes are integer numbers that indicate that a script has been successfully executed. These codes are also known as return codes or exit statuses. Exit codes usually return zero upon successful execution and non-zero upon unsuccessful execution.

However, many Bash script users want to return with different return codes on exit, but they get errors. In this tutorial, we will explain the different approaches to make a Bash script return with different return codes on exit.

Bash Script Returns with Different Return Codes on Exit

Before moving out to the methods, let’s take a look at the exit codes that have specific meanings:

Exit Codes Description
0 The script is executed successfully.
1 The script is executed with general errors.
2 Invalid use of some built-in commands in the script.
126 Shows the error for the command which is invoked and cannot be executed.
127 The command doesn’t exist in the script.
128 Shows the out-of-range exit code or fatal error signal.
130 CTRL+C terminates the script.
255 A general failure error code of the script.

How to Get Return Codes on Exit?

You only need to write the “echo $?” command to get the return code. For example, you want to compare two numbers using the following Bash script:

Once you execute the script in the terminal, run “echo $?” to get the return code on exit:


echo $?

The “comparison.sh” is executed successfully. That’s why terminals show zero as the return code. Similarly, you will get non-zero as the successful execution of the script. For example, if you use the Ls instead of the ls command in the script, you may get the non-zero as the return code:

As you can see in the previous image, the terminal shows 127 as the return code because the script contained the wrong command:

Make a Bash Script Return with Different Exit Codes

You can manually set up the exit codes in the script. For example, if you want to get 255 as the exit code, use the following script:

Now, execute the script and then run the “echo $?” command to get 255 as the return code:


echo $?


This is all about the exit codes that you may get after executing the Bash script in Linux. Exit codes help a user to identify the status of the Bash script. You can also manually set up and use the different return codes. Hence, you can get a non-zero exit code instead of zero even if the script is executed successfully. If you want to know more about the Bash scripts, browse our official website.

Share Button

Source: linuxhint.com

Leave a Reply