Bash get exit code of command on a Linux / Unix

I am a new Linux system user. How do I get the exit code of a command? How do I get the exit code or status of Linux or Unix shell command and store it into a shell variable?

Introduction – Each Linux or Unix shell command returns a status when it terminates normally or abnormally. For example, if backup.sh script is unsuccessful, and it returns a code which tells the shell script to send an e-mail to sysadmin.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash on Linux, macOS or Unix-like OS
Est. reading time 2 minutes

What is an exit code in bash shell?

Every Linux or Unix command executed by the shell script or user has an exit status. Exit status is an integer number. 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was a failure.

How to find out the exit code of a command

You need to use a particular shell variable called $? to get the exit status of the previously executed command. To print $? variable use the echo command or printf command:
date
echo $?
date-foo-bar
printf '%d\n' $?

Bash get exit code of command

How to get the exit code of a command such as date and date-foo-bar

From the above outputs, it is clear that the exit code is 0 indicates that date command was successful. Further, the exit code is 127 (non-zero) as the nonexistant-command was not successful.

Bash get exit code of command – How to use exit codes in shell scripts

So how do you store exit status of the command in a shell variable? Simply assign $? to a shell variable. The syntax is:

command
status=$?
## run date command ##
cmd="date"
$cmd
## get status ##
status=$?
## take some decision ## 
[ $status -eq 0 ] && echo "$cmd command was successful" || echo "$cmd failed"

How do I set an exit code for my own shell scripts?

The exit command cause normal shell script termination. It exits the shell with a status of N. The syntax is:

exit N
exit 1
exit 999

For example:

#!/bin/bash
/path/to/some/command
[ $? -eq 0 ]  || exit 1

A sample shell script to get the exit code of a command

#!/bin/bash
#
# Sample shell script to demo exit code usage #
#
set -e
 
## find ip in the file ##
grep -q 192.168.2.254 /etc/resolv.conf
 
## Did we found IP address? Use exit status of the grep command ##
if [ $? -eq 0 ]
then
  echo "Success: I found IP address in file."
  exit 0
else
  echo "Failure: I did not found IP address in file. Script failed" >&2
  exit 1
fi

Recommend exit code for your shell scripts

Exit Codes With Special Meanings
Exit status Description
1 Catchall for general errors
2 Misuse of shell builtins (according to Bash documentation)
126 Command invoked cannot execute
127 Command not found
128 Invalid argument to exit command
128+n Fatal error signal “n”
130 Bash script terminated by Control-C
255* Exit status out of range

How to deal with the exit codes of all piped commands

See “Bash Find out the exit codes of all piped commands” for more info.

Conclusion

This page showed how to use exit codes on Linux or Unix based system and how to get the exit status/code of command. For more info see:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Riya Joshi Mar 26, 2021 @ 18:53

    One of the best Unix exit code tutorial with examples. Thank you. I used it on my macOS macMini desktop.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum