What command should I use to display the exit code of the previous command?

Tutorial on using exit codes from Linux or UNIX commands. Examples of how to get the exit code of a command, how to set the exit code and how to suppress exit codes.

Estimated reading time: 3 minutes

Table of contents

What command should I use to display the exit code of the previous command?

What is an exit code in the UNIX or Linux shell?

An exit code, or sometimes known as a return code, is the code returned to a parent process by an executable. On POSIX systems the standard exit code is 0 for success and any number from 1 to 255 for anything else.

Exit codes can be interpreted by machine scripts to adapt in the event of successes of failures. If exit codes are not set the exit code will be the exit code of the last run command.

How to get the exit code of a command

To get the exit code of a command type echo $? at the command prompt. In the following example a file is printed to the terminal using the cat command.

cat file.txt
hello world
echo $?
0

The command was successful. The file exists and there are no errors in reading the file or writing it to the terminal. The exit code is therefore 0.

In the following example the file does not exist.

cat doesnotexist.txt
cat: doesnotexist.txt: No such file or directory
echo $?
1

The exit code is 1 as the operation was not successful.

How to use exit codes in scripts

To use exit codes in scripts an

cat doesnotexist.txt
cat: doesnotexist.txt: No such file or directory
echo $?
1
2 statement can be used to see if an operation was successful.

#!/bin/bash

cat file.txt 

if [ $? -eq 0 ]
then
  echo "The script ran ok"
  exit 0
else
  echo "The script failed" >&2
  exit 1
fi

If the command was successful the exit code will be 0 and ‘The script ran ok’ will be printed to the terminal.

How to set an exit code

To set an exit code in a script use

cat doesnotexist.txt
cat: doesnotexist.txt: No such file or directory
echo $?
1
4 where 0 is the number you want to return. In the following example a shell script exits with a 1. This file is saved as
cat doesnotexist.txt
cat: doesnotexist.txt: No such file or directory
echo $?
1
7.

#!/bin/bash

exit 1

Executing this script shows that the exit code is correctly set.

bash exit.sh
echo $?
1

What exit code should I use?

The Linux Documentation Project has a list of reserved codes that also offers advice on what code to use for specific scenarios. These are the standard error codes in Linux or UNIX.

  • 1 - Catchall for general errors
  • cat doesnotexist.txt
    cat: doesnotexist.txt: No such file or directory
    echo $?
    1
    
    9 - Misuse of shell builtins (according to Bash documentation)
  • #!/bin/bash
    
    cat file.txt 
    
    if [ $? -eq 0 ]
    then
      echo "The script ran ok"
      exit 0
    else
      echo "The script failed" >&2
      exit 1
    fi
    
    0 - Command invoked cannot execute
  • #!/bin/bash
    
    cat file.txt 
    
    if [ $? -eq 0 ]
    then
      echo "The script ran ok"
      exit 0
    else
      echo "The script failed" >&2
      exit 1
    fi
    
    1 - “command not found”
  • #!/bin/bash
    
    cat file.txt 
    
    if [ $? -eq 0 ]
    then
      echo "The script ran ok"
      exit 0
    else
      echo "The script failed" >&2
      exit 1
    fi
    
    2 - Invalid argument to exit
  • #!/bin/bash
    
    cat file.txt 
    
    if [ $? -eq 0 ]
    then
      echo "The script ran ok"
      exit 0
    else
      echo "The script failed" >&2
      exit 1
    fi
    
    3 - Fatal error signal “n”
  • #!/bin/bash
    
    cat file.txt 
    
    if [ $? -eq 0 ]
    then
      echo "The script ran ok"
      exit 0
    else
      echo "The script failed" >&2
      exit 1
    fi
    
    4 - Script terminated by Control-C
  • #!/bin/bash
    
    cat file.txt 
    
    if [ $? -eq 0 ]
    then
      echo "The script ran ok"
      exit 0
    else
      echo "The script failed" >&2
      exit 1
    fi
    
    5 - Exit status out of range

How to suppress exit statuses

Sometimes there may be a requirement to suppress an exit status. It may be that a command is being run within another script and that anything other than a 0 status is undesirable.

In the following example a file is printed to the terminal using cat. This file does not exist so will cause an exit status of 1.

To suppress the error message any output to standard error is sent to

#!/bin/bash

cat file.txt 

if [ $? -eq 0 ]
then
  echo "The script ran ok"
  exit 0
else
  echo "The script failed" >&2
  exit 1
fi
8 using
#!/bin/bash

cat file.txt 

if [ $? -eq 0 ]
then
  echo "The script ran ok"
  exit 0
else
  echo "The script failed" >&2
  exit 1
fi
9.

If the cat command fails an

#!/bin/bash

exit 1
0 operation can be used to provide a fallback -
#!/bin/bash

exit 1
1. In this case an exit code of 0 is returned even if there is an error.

Combining both the suppression of error output and the

#!/bin/bash

exit 1
0 operation the following script returns a status code of 0 with no output even though the file does not exist.

Which command is used to know the exit status?

gives the exit status of the last command executed. After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error. #!/bin/bash echo hello echo $?

How to check shell script exit code?

If you need to test the return code of a command you invoked on your shell script, you just need to test the $? variable immediately after the command executes. In this example, after invoking my command or script, I saved the exit code from $? on a variable for further utilization. (Again, $?

How to check the status of a previous command in Linux?

Using a Reverse Search of Linux Command History.
Press ctrl and r enters the reverse search mode, you should see the prompt now reads (reverse I search)`':.
Type a search term and you should see the last command issued that contained this term. ... .
Repeat pressing ctrl and r to step through other results..

Is exit status of last command?

Every UNIX/Linux command executed by the shell script or user leaves an exit status. It's an integer number that remains unchanged unless the next command is run. If the exit code is 0, then the command was successful.