In this article, we will explore different ways to check the exit status of a command in Bash, including using the $?
special variable, using the &&
and ||
operators, and using scripts and functions. Understanding how to check the exit status of a command in Bash can be helpful in various scenarios when working with Bash.
Checking If a Command Succeeded in Bash
In Bash, you can check if a command succeeded or failed by examining the exit status of the command. The exit status of a command is a numerical value that indicates the success or failure of the command. A command with an exit status of 0 indicates success, and a command with a non-zero exit status indicates failure.
To check the exit status of a command in Bash, you can use the $? special variable, which stores the exit status of the most recently executed command.
For example, consider the following command:
This command lists the contents of the /etc
directory in long format. To check if this command succeeded, you can use the following code:
This code checks the exit status of the ls command by examining the value of the $? special variable. If the value of $?
is 0
, the command succeeded, and the code prints “Command succeeded”. If the value of $?
is non-zero, the command failed, and the code prints “Command failed”.
Check Exit Status of a Function
You can also use the $?
special variable to check the exit status of a command in a script or function. For example, consider the following script:
This script defines a function check_command that takes a command as an argument and checks the exit status of the command. The function returns 0
if the command succeeded and 1
if the command failed. The script then calls the check_command function with the ls command and checks the exit status of the function by examining the value of the $?
special variable. If the value of $?
is 0
, the command succeeded, and the script prints “Command succeeded”. If the value of $?
is 1
, the command failed, and the script prints “Command failed”.
Check Exit Status Using &&
And ||
in Bash
In addition to using the $?
special variable, you can also use the &&
and ||
operators to check the exit status of a command and execute different commands based on the result. For example, consider the following code:
This code checks the exit status of the ls
‘ command and executes different commands based on the result. If the ls command succeeded, the code prints “Command succeeded”. If the ls command failed, the code prints “Command failed”.
Conclusion
In conclusion, checking if a command succeeded or failed in Bash is a useful task that can be accomplished by examining the exit status of the command. The exit status of a command is a numerical value that indicates the success or failure of the command. A command with an exit status of 0
indicates success, and a command with a non-zero exit status indicates failure. To check the exit status of a command in Bash, you can use the $? special variable, which stores the exit status of the most recently executed command.
You can also use the $? special variable to check the exit status of a command in a script or function or use the &&
and ||
operators to check the exit status and execute different commands based on the result. Understanding how to check the exit status of a command in Bash can be helpful in various scenarios, such as when you want to execute different commands based on the success or failure of a command or when you want to perform error handling in a script.