Home arrow BrainDump arrow Page 3 - Better Command Execution with bash

4.8 Displaying Error Messages When Failures Occur - BrainDump

In this second part of a two-part series on executing commands with the bash shell, you will learn how to use fewer if statements, display error messages when failures occur, and more. This article is excerpted from chapter four of the bash Cookbook, Solutions and Examples for bash Users, written by Carl Albing, JP Vossen and Cameron Newham (O'Reilly, 2007; ISBN: 0596526784). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. Better Command Execution with bash
  2. 4.7 Running Long Jobs Unattended
  3. 4.8 Displaying Error Messages When Failures Occur
  4. 4.9 Running Commands from a Variable
  5. 4.10 Running All Scripts in a Directory
By: O'Reilly Media
Rating: starstarstarstarstar / 5
June 19, 2008

print this article




You need your shell script to be verbose about failures. You want to see error messages when commands don’t work, but if statements tend to distract from the visual flow of statements.


A common idiom among some shell programmers is to use the || with commands to spit out debug or error messages. Here’s an example:

  cmd || printf "%b" "cmd failed. You're on your own\n"


Similar to how the && didn’t bother to evaluate the second expression if the first was false, the || tells the shell not to bother to evaluate the second expression if the first one is true (i.e., succeeds). As with &&, the||syntax harkens back to logic and C Language where the outcome is determined (as true) if the first expression inA OR Bevaluates to true—so there’s no need to evaluate the second expression. In bash, if the first expression returns0(i.e., succeeds) then it just continues on. Only if the first expression (i.e., exit value of the command) returns a non-zero value must it evaluate the second part, and thus run the other command.

Warning—don’t be fooled by this:

  cmd || printf "%b" "FAILED.\n" ; exit 1

The exit will be executed in either case! The OR is only between those two commands. If we want to have the exit happen only on error, we need to group it with the printf so that both are considered as a unit. The desired syntax would be:

  cmd || { printf "%b" "FAILED.\n" ; exit 1 ; }

Due to an oddity of bash syntax, the semicolon after the last command and just before the } is required, and that closing brace must be separated by whitespace from the surrounding text.

See Also

>>> More BrainDump Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Apple Founder Steve Jobs Dies
- Steve Jobs` Era at Apple Ends
- Google's Chrome Developer Tool Updated
- Google's Chrome 6 Browser Brings Speed to th...
- New Open Source Update Fedora 13 is Released...
- Install Linux with Knoppix
- iPad Developers Flock To SDK 3.2
- Managing a Linux Wireless Access Point
- Maintaining a Linux Wireless Access Point
- Securing a Linux Wireless Access Point
- Configuring a Linux Wireless Access Point
- Building a Linux Wireless Access Point
- Migrating Oracle to PostgreSQL with Enterpri...
- Demystifying SELinux on Kernel 2.6
- Yahoo and Microsoft Create Ad Partnership

Developer Shed Affiliates


Dev Shed Tutorial Topics: