Understanding Exit Codes and Using them in Bash scripts

Photo by Alex Holyoake

What are exit codes?

On Unix and Linux systems, programs can pass a value to their parent process while terminating. This value is referred to as an exit code or exit status. On POSIX systems the standard convention is for the program to pass 0 for successful executions and 1 or higher for failed executions.

What happens if I don’t specify an exit code

In Linux any script run from the command line has an exit code. With Bash scripts, if the exit code is not specified in the script itself the exit code used will be the exit code of the last command run. To help explain exit codes a little better we are going to use a quick sample script.

#!/bin/bash
touch /root/test
echo created file
$ ./tmp.sh
touch: cannot touch '/root/test': Permission denied
created file
$ echo $?
0
#!/bin/bash
touch /root/test
$ ./tmp.sh
touch: cannot touch '/root/test': Permission denied
$ echo $?
1

Using exit codes in your bash scripts

While removing the echo command from our sample script worked to provide an exit code, what happens when we want to perform one action if the touch was successful and another if it was not. Actions such as printing to stdout on success and stderr on failure.

Testing for exit codes

Earlier we used the $? special variable to print the exit code of the script. We can also use this variable within our script to test if the touch command was successful or not.

#!/bin/bash touch /root/test 2> /dev/nullif [ $? -eq 0 ] 
then
echo "Successfully created file"
else
echo "Could not create file" >&2
fi
$ ./tmp.sh 
Could not create file

Providing your own exit code

While the above revision will provide an error message if the touch command fails, it still provides a 0 exit code indicating success.

$ ./tmp.sh 
Could not create file
$ echo $?
0
#!/bin/bashtouch /root/test 2> /dev/null if [ $? -eq 0 ] 
then
echo "Successfully created file"
exit 0
else
echo "Could not create file" >&2
exit 1
fi
$ ./tmp.sh 
Could not create file
$ echo $?
1

Using exit codes on the command line

Now that our script is able to tell both users and programs whether it finished successfully or unsuccessfully we can use this script with other administration tools or simply use it with bash one liners.

$ ./tmp.sh && echo "bam" || (sudo ./tmp.sh && echo "bam" || echo "fail") 
Could not create file
Successfully created file
bam

More exit codes

The exit command in bash accepts integers from 0 - 255, in most cases 0 and 1 will suffice however there are other reserved exit codes that can be used for more specific errors. The Linux Documentation Project has a pretty good table of reserved exit codes and what they are used for.

--

--

Building payments systems at American Express. Author (https://amzn.to/3kuCFpz). Open-source contributor (https://github.com/madflojo).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store