Bash Exit Codes: A Complete Guide

bash exit code

When working with Bash scripts, it is essential to understand the concept of exit codes. An exit code is a number that a command or script returns to the operating system upon completion of its execution. It indicates whether the command or script completed successfully or failed and provides additional information about the outcome of the operation. In this guide, we’ll explore the basics of Bash exit codes, how to use them in scripts, and related concepts that will help you write efficient and reliable scripts.

Basics of Bash Exit Codes

In Bash, an exit code is an integer value between 0 and 255 that a command or script returns to the shell. By convention, an exit code of 0 means success, and any non-zero value means failure. Some commonly used exit codes are:

  • Exit code 0: Success
  • Exit code 1: General error
  • Exit code 2: Misuse of shell built-ins
  • Exit code 126: Command cannot execute
  • Exit code 127: Command not found
  • Exit code 128: Invalid argument to exit

Using Exit Codes in Bash Scripts

In Bash scripts, you can use exit codes to determine the success or failure of a command or script and take appropriate actions based on the outcome. For example, you can use the if statement to check the exit code of a command and execute different commands based on the result.

Here’s an example:

#!/bin/bash

ls /usr/bin

if [ $? -eq 0 ]; then
    echo "Command succeeded"
else
    echo "Command failed"
fi

In this script, we run the ls command on the /usr/bin directory and check its exit code using the $? variable. If the exit code is 0, we print “Command succeeded,” and if it’s non-zero, we print “Command failed.”

You can also use exit codes to terminate a script or function prematurely. For example, you can use the exit command to exit a script with a specific exit code.

Here’s an example:

#!/bin/bash

function my_function {
    # Do some work
    if [ some condition ]; then
        exit 1
    fi
    # Do more work
}

my_function

if [ $? -eq 0 ]; then
    echo "Function succeeded"
else
    echo "Function failed"
fi

In this script, we define a function my_function that performs some work and checks a condition. If the condition is true, we use the exit command to exit the function with an exit code of 1. We then check the exit code of the function using $? and print the appropriate message.

$? Variable

As mentioned earlier, the $? variable is a special variable that stores the exit code of the last command executed in Bash. You can use this variable to check the success or failure of a command and take appropriate actions based on the result.

trap Command

The trap command is a Bash built-in command that allows you to execute a command when a signal is received. You can use the trap command to handle errors or unexpected events in your script and take appropriate actions based on the situation.

Here’s an example:

#!/bin/bash

function cleanup {
    echo "Cleaning up"
    # Do some cleanup work
}

trap cleanup EXIT

# Do some work

exit 0

In this script, we define a function cleanup that performs some cleanup work when the script exits. We use the trap command to execute this function when the script receives the EXIT signal. This ensures that the cleanup code is always executed, even if the script fails or is terminated prematurely.

Conclusion

Exit codes are an essential concept in Bash scripting that allow you to determine the success or failure of a command or script and take appropriate actions based on the outcome. By understanding how to use exit codes in your scripts, you can write efficient and reliable code that handles errors and unexpected events gracefully.