$?” is a built-in variable that your shell uses to store the exit status code of the last executed command in integer format and remains unchanged unless the next command is executed.
Using this exit status code, you can debug the problem that occurred while executing the command, which can be extremely beneficial in shell script error handling.
The following is the list of known exit status codes from total (
0-255) for bash in Linux:
|If no command is executed or a command is successfully executed, its status is set to zero.|
|It will catch the general errors.|
|It will catch the shell built-in errors.|
|Commands invoked cannot be executed.|
|Command not found.|
|Invalid argument to exit.|
|Fatal error signal “n”.|
|The script was terminated by “Control+C“.|
|The exit status is out of range.|
Checking the Bash Exit Status Code
The following is a demonstration of the known bash exit status code:
Example of Exit Status Code “0”
By default, when you start or restart your system (or shell session) without executing any commands, the exit status code is set to zero.
$ echo $?
Even if you execute any command with successful output, the exit status will remain zero.
$ pwd $ echo $?
Example of Exit Status Code “1”
If you pass a wrong or invalid option to the command, it will set the exit status code to a non-zero value.
$ cat -a file.txt $ echo $?
The above exit status code “
1” represents that the last executed command provided an invalid option.
Example of Exit Status Code “2”
If you pass a wrong or invalid argument to the command, it will set the exit status code to “
$ ls wrongfile $ echo $?
Example of Exit Status Code “127”
If you mistyped or executed the wrong command, it will set the exit status code to “
$ wrongcommand $ echo $?
Example of True or False Keywords
Even if you execute keywords like “
true“, it will set the exit status code to “
0“, and for “
false“, the value will be set to “
1“, making it highly effective while writing a shell script.
$ true $ echo $? $ false $ echo $?
Checking the Exit Status Code of Piped Commands
If you have piped one or more commands together, the exit status will store the value of the last executed command.
For the following piped commands, it will show you the exit status code for the PWD command.
$ WRONGCOMMAND | pwd $ echo $?
Controlling the Flow of Script Using the Exit Status Code
The exit status code can be extremely helpful while writing shell scripts to handle the flow of your script with all possible errors.
For example, the following is a basic demonstration of handling a script using the exit status code:
#!/bin/bash echo "Its, Linux TLDR" status=$? [ $status -eq 0 ] && echo "command succeeded" || echo "command failed"
If you run the above script, it will print “
command succeeded” on an exit status code of “
0” and “
command failed” on a non-zero exit status code.
However, if you modify the same script and misspell or include a wrong command, as shown.
#!/bin/bash WRONG-COMMAND status=$? [ $status -eq 0 ] && echo "command succeeded" || echo "command failed"
It will print “
command failed” rather than “
The Meaning of the Exit Status Code Value Might Be Different for Different Commands
Both commands have different explanations for exit status codes “
1” and “
The following is the exit status code of the ls command.
The following is the exit status code of the grep command.
So, before assigning the condition using the exit status code of commands, I strongly recommend that you check their manual page or search for the command on our site (most probably you will find it) to find the meaning of their default exit status code.
If you are too lazy to do that, then define your own exit status code.
Defining the Custom Exit Status Code in Script
To remove the headache of different commands having different meanings for exit status codes, you can manually specify your own custom exit status code for a specified command (or condition) using the exit keyword.
As per rule, keep the “
0” value reserved for “
successful execution” and use the value between the range of (
1-255) for errors.
The following is a basic script that will check if the current user is root or not; if the user is root, it will print “
Root user” on screen with an exit status code of “
0” or “
Not root user” with an exit status code of “
#!/bin/bash if [[ "$(whoami)" == "root" ]] then echo "Root user" exit 0 else echo "Not root user" exit 1 fi
Executing the above script with and without the root user.
#Executing as a non-root user $ ./script.sh $ echo $? #Executing as a root user $ sudo ./script.sh $ echo $?
And that was the end of this guide.
If you have any suggestions or good examples that need to be added to this article, please share them with us in the comment section.