Bash Exit Status Codes

In this article we will go over the built-in bash exit command as well as the exit status codes of the commands that have been executed.
Bash Exit Status Codes

On this page

When writing Bash scripts, you will frequently need to stop the execution of a script when a certain condition is met or perform some actions based on a command's exit code.

In this article we will go over the built-in bash exit command as well as the exit status codes of the commands that have been executed.

Exit Status Code

When a shell command exits, whether successfully without any errors or unsuccessfully with errors, it returns an exit code.

An exit code of zero indicates that the command was completed properly without any errors, while a non-zero indicates that an error occurred.

The $? is a special shell variable that stores the exit status of the most recently run command:

cat manifesto.txt

echo $?
# output

A Gentle reminder, I use Arch BTW!

0

Because the cat command was completed successfully and without error, the exit code is zero, as expected.

If you attempt to run cat command on a not-existing file, the exit code will be non-zero as shown below:

cat no-file

echo $?
# output

cat: no-file: No such file or directory

1

As expected, the exist status code is non-zero.

The exit status code of a command can be used for debugging and determining the reason for its failure. The man pages for each command provide information about the exit codes. When chaining commands using pipes, the exit status code is that of the last command in the chain.

Consider the following example:

ls -lah | head -n 4

echo $?
# output

total 88K
drwxr-x--- 5 mint mint 4.0K Mar 13 22:24 .
drwxr-xr-x 5 root root 4.0K Nov 22 13:54 ..
-rw------- 1 mint mint 5.6K Mar  5 17:20 .bash_history

0

In the preceding example, the echo $? command prints the exit status code of the head command, which is zero. However, this does not prove that the exit status code is that of the last command.

Let's look at another example where we execute the ls command on a non-existent directory:

Let's look at another example where we execute the ls command on a non-existent directory.

ls -lah no-directory | head -n 4

echo $?
# output

ls: cannot access 'no-directory': No such file or directory

0

Here, you can see that the ls command fails, but the exit status code displayed is still zero. This proves that the exit status code is indeed that of the last command when pipes are used to chain multiple commands.

Bash built-in exit command

Now that you know what command exist status codes are, let's look at how to arbitrary set them using the bash exit command.

The exit command is used to exit the current shell. It takes a number as a parameter and exits the shell with the status of the number you passed as a parameter value.

If no parameters were supplied, it would return the status of the most recently executed command.

$ exit N

The exit command in shell scripts can be used to return an exit status code to the shell. When an argument is passed to the exit command, that value is returned as the exit code.

Here's an example script that demonstrates the usage of the exit command:

#!/bin/bash

# Filename: checking if a specific file exists

FILE=~/logs.txt

if test -f "$FILE"
then
  echo "$FILE exists."
  exit 2
else
  echo "$FILE doesn't exists."
  exit 4
fi

After executing the above script, running echo $? will output 2 if the file logs.txt exists, or 4 if it does not exist.

If a script ends without specifying an argument to the exit command, the exit code is the status of the last command executed in the script. Consider the following example:

#!/bin/bash

# Filename: checking if a specific file exists

FILE=~/logs.txt

if test -f "$FILE"
then
  echo "$FILE exists."
  exit 
else
  echo "$FILE doesn't exists."
  touch logs.txt
  exit
fi

In this case, if the file logs.txt exists, running echo $? after executing the script will output the exit status of the echo "$FILE exists." command. If the file does not exist, it will output the exit status of the touch logs.txt command.

Not specifying a value to the exit command is equivalent to exit $? or leaving the script without an explicit exit command.

Summing up

In conclusion, understanding exit status codes in Bash scripts is crucial for error handling and debugging. By utilizing the exit command, you can set custom exit codes to indicate success or failure based on specific conditions within your script.

Subscribe to sysxplore newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!