$?” 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:
|0||If no command is executed or a command is successfully executed, its status is set to zero.|
|1||It will catch the general errors.|
|2||It will catch the shell built-in errors.|
|126||Commands invoked cannot be executed.|
|127||Command not found.|
|128||Invalid argument to exit.|
|128+n||Fatal error signal “n”.|
|130||The script was terminated by Control+C.|
|255||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 $? 0
Even if you execute any command with successful output, the exit status will remain zero.
$ pwd /home/linuxtldr $ echo $? 0
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 cat: invalid option -- 'a' Try 'cat --help' for more information. $ echo $? 1
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 “2“.
$ ls wrongfile ls: cannot access 'wrongfile': No such file or directory [email protected]:~$ echo $? 2
Example of Exit Status Code “127”
If you mistyped or executed the wrong command, it will set the exit status code to “127“.
$ wrongcommand wrongcommand: command not found $ echo $? 127
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 $? 0 $ false $ echo $? 1
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 /home/linuxtldr WRONGCOMMAND: command not found $ echo $? 0
Also Read: Beginners Guide for Set Command in Linux
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:
$ cat script.sh #!/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.
$ ./script.sh Its, Linux TLDR command succeeded
However, if you modify the same script and misspell or include a wrong command, as shown.
$ cat script.sh #!/bin/bash WRONG-COMMAND status=$? [ $status -eq 0 ] && echo "command succeeded" || echo "command failed"
It will print “command failed” rather than “command succeeded”.
$ ./script.sh ./script.sh: line 3: WRONG-COMMAND: command not found command unsuccessful
The Meaning of the Exit Status Code Value Might Be Different for Different Commands
Most of the commands, like echo or cat, follow the standard exit status code. However, the meaning of the exit status code value might vary for other commands like ls and grep.
Both commands have different explanations for exit status codes “1” and “2“.
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 “1“.
$ cat script.sh #!/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 Not root user $ echo $? #Verifying the result 1 #Executing as a root user $ sudo ./script.sh Root user $ echo $? #Verifying the result 0
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.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.