Work the Shell - Understanding Exit Codes
Last month, we looked at signals, the rudimentary mechanism that processes on a Linux box can use to communicate events and state changes. We talked about how each of the signals can be sent manually to a running process with the kill command, and how shell scripts then can catch and respond to specific signals (though not all of them—some cannot be caught because they're actually handled by the operating system itself).
Analogous to signals, exit codes turn out to be an easy way for processes to communicate state back to the calling parent process, in a way that most Linux users just ignore. Not anymore—this month, we're going to take a closer look.
Let's start with a simple Linux command that everyone's probably already mastered: mv, which moves a file or directory from one spot in the filesystem to another (and/or renames it).
As you know, you can generate errors if the target is missing, destination is missing and so on. Here's quick example:
$ mv missing ~/missing2 mv: cannot move `missing' to `.../missing2': No such file or directory
You see an error; obviously, it didn't work. Ah, but behind the scenes, a numeric “return code” variable has been set in the shell too, something you can test and respond to within a shell script. Check out this sequence:
$ echo $? 0 $ mv missing ~/missing2 mv: cannot move `missing' to `.../missing2': No such file or directory $ echo $? 1 $ echo test me test me $ echo $? 0
If no error occurs when executing a command, the exit code (which we reference in the shell with the shorthand $?) will have the value of 0: no error. Now, if I run a command that fails, the exit code will have a nonzero value. In the case of the failing mv command above, the error code will have the value of 1. And, if I now run yet another command, one which runs without error, the error code is reset to zero.
Now, let's take a peek at the mv man page, paying particular attention to the latter part of the doc. Close examination reveals: “The mv utility exits 0 on success, and >0 if an error occurs.”
That's not so interesting, really. The grep command has more interesting diagnostics, actually: “Normally, exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred, unless the -q or --quiet or --silent option is used and a selected line is found.”
There is a set of system exit codes that are defined, although it's possible you'll never need the information. Here's a list of the codes and their meanings:
1: general errors
2: misuse of shell builtins (pretty rare)
126: cannot invoke requested command
127: command not found error
128: invalid argument to “exit”
128+n: fatal error signal “n” (for example, kill -9 = 137).
130: script terminated by Ctrl-C
I'd never actually seen this list until I started digging into the issue of exit codes, so you can continue on your merry shell-scripting path safely without worrying about the data above.
The most common situation in which you analyze and respond to an exit code is in error handling in a script.
Here's a simple snippet where you want to create a directory. If it fails, you want to output an error message and quit:
#!/bin/sh mkdir /usr echo \$? = $? if [ $? -ne 0 ] ; then echo "mkdir /usr failed: we have an exit code of $?" exit 1 fi echo "made the requested directory. Why is '/' world writable?" exit 0
It turns out, there's a nuance to working with the $? that I've already alluded to—one that makes output statements like the first “echo” quite problematic. You can see why in the output:
$ ./test.sh mkdir: /usr: File exists $? = 1 made the requested directory. Why is '/' world writable?
Can you see what happened? The exit code = 1 immediately after the mkdir, which makes sense as /usr already exists, but when we again test the exit code in the conditional, it's not a nonzero value!
Why? Because at that point, it indicates the exit code of the echo statement, not the mkdir command. Oops.
You can verify this simply by commenting out the first echo statement, in which case you now see this as the command output:
$ !. ./test.sh mkdir: /usr: File exists mkdir /usr failed: we have an exit code of 0
That makes more sense, doesn't it?
Because this can be tricky, a common thing I see in really bulletproof shell scripts with lots of error handling is something like this:
#!/bin/sh mkdir /usr error=$? if [ $error -ne 0 ] ; then echo "mkdir /usr failed: we have an exit code of $error" exit 1 fi
This is one instance where a local variable to hold a system or global variable makes a lot of sense, and it also lets you do things like have an error message show up on-screen and be handed off to something like syslog() to ensure that the admin sees it at some point.
Of course, error handling doesn't always just need to print a message and exit. Another scenario might be the following:
alternates='
http://www.example.com/test.pdf
http://www.example2.com/test.pdf
http://www.example3.com/test.pdf
'
gotit=0
for file in $alternates
do
wget $file
if [ $? -ne 0 ]; then
echo "Unable to get $file
else
gotit=1
break
fi
done
...
Here, we try to retrieve a file from one of multiple alternate locations and exit the loop only when we succeed (or when we've run out of possibilities).
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




12 min 25 sec ago
6 hours 4 min ago
10 hours 35 min ago
10 hours 36 min ago
12 hours 36 min ago
21 hours 22 min ago
21 hours 56 min ago
22 hours 54 min ago
23 hours 44 min ago
1 day 3 hours ago