What is Exit Status Code ($?) of Last Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The “$?” 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:

Exit statusDescription
0If no command is executed or a command is successfully executed, its status is set to zero.
1It will catch the general errors.
2It will catch the shell built-in errors.
126Commands invoked cannot be executed.
127Command not found.
128Invalid argument to exit.
128+nFatal error signal “n”.
130The script was terminated by “Control+C“.
255The 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 $?

Output:

Checking the zero exit status code in terminal

Even if you execute any command with successful output, the exit status will remain zero.

$ pwd
$ echo $?

Output:

Checking the zero exit status code after successfully running a command in terminal

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 $?

Output:

Exit status code one occur after running wrong command

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
$ echo $?

Output:

Exit status code two occur after passing wrong option to the command

Example of Exit Status Code “127”

If you mistyped or executed the wrong command, it will set the exit status code to “127“.

$ wrongcommand
$ echo $?

Output:

Exit status code 127 occur after running wrong command

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 $?

Output:

Checking exit status after issuing true and false keyword

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 $?

Output:

Exit status code zero appeared after piping wrong command to correct command

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.

$ ./script.sh 

Output:

Running a script demonstration the flow control using 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 “command succeeded“.

$ ./script.sh 

Output:

Running a script demonstration the flow control using exit status code after wrong command appeared

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.

ls command exit status code

The following is the exit status code of the grep command.

grep command exit status code

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“.

#!/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 $?

Output:

Creating a script that should be run only with root privileges otherwise exit status code 1 will appear and on success zero will appear

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.

Sayonara!

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.