Work the Shell - Understanding Exit Codes

In the real world, things don't always work out as you hoped. That's what exit codes are for—letting you know when things went wrong.
Hiding Error Messages

Now that you know how to capture and analyze the exit codes from system commands, what if you want to have the error message be one from your script, not one from the command itself?

That's done with another new shorthand notation: >&, which redirects the stderr/error output stream. Here's how I use that to hide all error messages from the mkdir command being used in our sample scripts:

mkdir /usr >& /dev/null

You also can use &> or 2>&1 instead of >&.

If you don't test the results of the command, of course, you seriously can hose things up, but this makes the output more elegant for sure:

$ ./
mkdir /usr failed: we have an exit code of 0

Hmmm...I'm still getting that false 0. Oh! I haven't added the code to save the exit code value as “error”. One slight tweak later and:

$ ./
mkdir /usr failed: we have an exit code of 1

That's more like it!

I'm going to call this a wrap for this month. Next month, I'll demonstrate how the exit command lets you send exit codes back to the calling program from procedures and functions, just as if they were separate Linux commands rather than part of the same shell script.

Dave Taylor has been hacking shell scripts for a really long time, 30 years. He's the author of the popular Wicked Cool Shell Scripts, and he can be found on Twitter as @DaveTaylor and more generally at


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

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState