Sep 12, 2023 3 min read

Bash Exit Command and Exit Codes

Use bash exit command and exit codes with our step-by-step tutorial. The Bash shell provides a way to terminate scripts with exit codes.

Bash Exit Command and Exit Codes
Table of Contents

Introduction

When building Bash scripts, you'll frequently need to end the script when a certain condition is met or take action based on a command's exit code.

In this tutorial, you will use the Bash exit command and the exit statuses of the executed commands. We will also address a few FAQs on Bash Exit Command and Exit Codes.

Exit Status

When a shell command completes successfully or poorly, it provides an exit code.
An exit code of zero indicates that the command was executed successfully, while a non-zero indicates that an error occurred.

The special variable $? returns the last performed command's exit status:

date &> /dev/null
echo $?

The date command returned a zero exit code, indicating that it was successful:

Output

0

If you try to execute ls on a directory that doesn't exist, you'll get a non-zero exit code:

ls /nonexisting_dir &> /dev/null
echo $?
Output

2

You can use the status code to figure out why the command failed. The man pages for each command give information about the exit codes.

When running a multi-command pipeline, the pipeline's exit state is determined by the last command:

sudo tcpdump -n -l | tee file.out
echo $?

In the preceding example, echo $? prints the tee command's exit code.

Bash exit Command

With the exit command, the shell is exited with a status of N. The syntax is as follows:

exit N

If N is omitted, the exit status code is the last command executed.

The value sent as an input to the exit command is returned to the shell as an exit code when used in shell scripts.

Examples

The exit status of instructions can be used in conditional commands like if. If the "search-string" is found in the filename, grep will exit with a zero (which implies true in shell scripting):

if grep -q "search-string" filename then
  echo "String found."
else
  echo "String not found."
fi

The exit status of the command controls whether the following command in the list is performed when a list of commands is separated by && (AND) or || (OR). Only if cd returns zero will the mkdir command be executed:

cd /opt/code && mkdir project

The script exit code is the last command executed in the script if it concludes with exit without giving a parameter.

#!/bin/bash

echo "doing stuff..."

exit

exit $? or omitting the exit is the same as using just exit.

Here's an example of how to stop the script from running if it's run by a non-root user:

#!/bin/bash

if [[ "$(whoami)" != root ]]; then
  echo "Only user root can run this script."
  exit 1
fi

echo "doing stuff..."

exit 0

The exit code will be 0 if you run the script as root. The script will quit with status 1 if this is not the case.

FAQs on Bash Exit Command and Exit Codes

How do I set an exit code in my Bash script?

To set an exit code in your Bash script, simply use the "exit" command followed by the desired exit code. For example, exit 0 sets the exit code to 0 (success).

What is the significance of exit code 0?

An exit code of 0 represents successful execution of a program or script. It indicates that the program completed its tasks without encountering any errors.

Can I customize the exit codes in my Bash script?

Yes, you can customize the exit codes in your Bash script by assigning different numerical values. However, it is good practice to follow convention, where exit code 0 represents success, and non-zero values indicate specific error conditions.

How can I check the exit code of a command or script?

The exit code of the previously executed command or script can be accessed by using the special variable $?. For example, echo $? will display the exit code.

What is the significance of non-zero exit codes?

Non-zero exit codes indicate that a command or script encountered some error or failure during execution. Different non-zero exit codes may have specific meanings, depending on the context and conventions of the script.

Can I use exit codes for error handling in Bash?

Yes, exit codes are commonly used for error handling in Bash scripts. You can check the exit code after executing a command and take appropriate actions like displaying an error message, exiting the script, or executing an alternative path.

How can I explain the meaning of exit codes in my script's documentation?

You can document the meanings of exit codes by defining them in comments or providing a separate document that explains the purpose and significance of each exit code used in your script.

Conclusion

When a shell command completes, it returns an exit code. The exit command exits a shell with a specified status.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.