How to Check the Exit Status of a Command in Bash
In Bash, every command returns an exit status (also known as return status or exit code) when it finishes executing. This exit status is a numeric value that indicates whether the command was successful or if an error occurred. Understanding how to check the exit status of commands is crucial for effective error handling in scripts. This guide will explain how to check the exit status, provide examples, and discuss best practices.
1. Understanding Exit Status
The exit status of a command is a number between 0
and 255
. By convention:
- 0: Indicates success. The command executed successfully without errors.
- 1 or any non-zero value: Indicates failure. The command encountered an error or did not complete successfully.
2. Checking the Exit Status
To check the exit status of the last executed command, you can use the special variable $?
. This variable holds the exit status of the most recently executed foreground command.
Example of Checking Exit Status
command
status=$?
echo "The exit status is: $status"
In this example:
command
represents any command you want to execute.- The exit status of that command is stored in the variable
status
. - The script then prints the exit status to the terminal.
3. Practical Example
Let’s look at a practical example using the ls
command to check for a directory that may or may not exist.
Example of Using ls
Command
directory="my_directory"
ls "$directory"
status=$?
if [ $status -eq 0 ]; then
echo "Directory exists."
else
echo "Directory does not exist."
fi
In this example:
- The script attempts to list the contents of
my_directory
. - The exit status is checked using
$?
. - If the exit status is
0
, it prints "Directory exists." Otherwise, it prints "Directory does not exist."
4. Using Conditional Statements
You can also use the exit status directly in conditional statements without explicitly storing it in a variable.
Example of Using Exit Status in an if
Statement
if ls "$directory"; then
echo "Directory exists."
else
echo "Directory does not exist."
fi
In this example:
- The
if
statement directly checks the exit status of thels
command. - If the command is successful, it prints "Directory exists." Otherwise, it prints "Directory does not exist."
5. Best Practices for Checking Exit Status
Here are some best practices for checking exit status in Bash scripts:
- Always Check Exit Status: After executing a command, check its exit status to handle errors appropriately.
- Use Meaningful Messages: Provide clear messages in your error handling to help users understand what went wrong.
- Combine with
set -e
: Consider usingset -e
in your scripts to automatically exit on any command failure, which can simplify error handling.
6. Conclusion
Checking the exit status of commands in Bash is essential for effective scripting and error management. By using the special variable $?
, you can determine whether a command succeeded or failed and take appropriate actions based on that information. Understanding and implementing these practices will enhance the reliability and robustness of your Bash scripts.