When writing a script or executing a command, it makes sense to know if it succeeded. In Linux, when a command is run, we get an exit code which confirms whether the executed command was successful. When running a command that relies on the output of the other command, you must fetch the exit code of the first command to determine whether the other command will run. This guide focuses on two ways of checking whether a command succeeded in Bash.
Two Ways of Checking If a Command Succeeded in Bash
When you run a command in Bash, it must return an exit code or status. You can use the “if” statement to evaluate the returned value of a given command or use the special variable ($?) to check the exit code or status of the executed command. Let’s get into detail.
1. Using the Bash If Statement
With the “if” statement, you can evaluate the return value of a command which is executed within an expression. The “if” statement fetches the exit status. Depending on your expression, it executes the consequential command in the if-else conditional statement.
For instance, we can create a script that deletes any text file in the current directory and return a success message if the command executes or a failure message if the command doesn’t succeed.
Make the script executable using the chmod command. Then, execute it.
Note that our command doesn’t succeed, thus displaying the failed message. However, if the command succeeds, we get a different output.
You can use the same concept when running any command in Bash such that if a given command doesn’t succeed, it displays a given output to confirm that the next command won’t execute since the first command already failed.
2. Using the Special Variable ($?)
The special variable ($?) returns the exit code of the last executed command. When it returns the exit code as zero, it implies that the command succeeded successfully. However, any other number means that your command didn’t succeed.
Using the special variable to check if a command succeeded successfully requires you to use a binary comparison operator (eq) to see if the exit code is zero. If not, it executes the else part of the if-else conditional statement.
Run the command in your script. Then, add an if-else conditional statement to check if your command was executed. Let’s edit our earlier example to use the special variable as follows:
If we run the script, it returns an output which confirms that it didn’t succeed since there are no text files to delete. Therefore, it executes the command in the else part instead of the “if”.
Suppose we have text files in the current directory. We could get a different output. Here, we get the success message which means that the exit code matched the “if” section as it succeeded.
In the previous case, the command succeeded. We can verify it based on the output message that it displays, confirming that the exit code for the command is zero.
There are two common ways of checking whether your command succeeded in Bash. You can use the conditional if-else statement to display an output, depending on whether a command succeeded. Alternatively, you can use the special variable ($?) to check the exit code or status of the executed command using the if-else statement.