Home arrow BrainDump arrow Better Command Execution with bash

Better Command Execution with bash

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



4.6 Using Fewer if Statements


As a conscientious programmer, you took to heart what we described in the previous recipe, Recipe 4.5. "Deciding Whether a Command Succeeds." You applied the concept to your latest shell script, and now you find that the shell script is unreadable, if with all those if statements checking the return code of every command. Isnít there an alternative?


Use the double-ampersand operator in bash to provide conditional execution:

  $ cd mytmp && rm *


Two commands separated by the double ampersands tells bash to run the first command and then to run the second command only if the first command succeeds (i.e., its exit status is 0). This is very much like using an ifstatement to check the exit status of the first command in order to protect the running of the second command:

  cd mytmp
  if (( $? )); then rm * ; fi

The double ampersand syntax is meant to be reminiscent of the logical and operator in C Language. If you know your logic (and your C) then youíll recall that if you are evaluating the logical expressionA AND B, then the entire expression can only be true if both (sub)expressionAand (sub)expressionBevaluate to true. If either one is false, the whole expression is false. C Language makes use of this fact, and when you code an expression likeif (A && B) { ... }, it will evaluate expressionAfirst. If it is false, it wonít even bother to evaluateBsince the overall outcome (false) has already been determined (byAbeing false).

So what does this have to do with bash? Well, if the exit status of the first command (the one to the left of the&&) is non-zero (i.e., failed) then it wonít bother to evaluate the second expressionói.e., it wonít run the other command at all.

If you want to be thorough about your error checking, but donít wantifstatements all over the place, you can have bash exit any time it encounters a failure (i.e., a non-zero exit status) from every command in your script (except inwhileloops andifstatements where it is already capturing and using the exit status) by setting the-eflag.

  set -e
  cd mytmp
  rm *

Setting the -e flag will cause the shell to exit when a command fails. If the cd fails, the script will exit and never even try to execute the rm * command. We donít recommend doing this on an interactive shell, because when the shell exits it will make your shell window go away.

See Also

ē Recipe 4.8, "Displaying Error Messages When Failures Occur" for an explanation of the || syntax, which is similar in some ways, but also quite different from the && construct

>>> 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: