[lug] How does bash "set -e" work?

Jeffrey S. Haemer jeffrey.haemer at gmail.com
Tue Jan 1 12:32:27 MST 2013


Lee,

On Tue, Jan 1, 2013 at 12:07 PM, Lee Woodworth <blug-mail at duboulder.com>wrote:

> At least with bash, subshells (without -e), command sequences and pipes
> appear to return the status of the last command:
>

Ab-so-tively. I set "pipefail" in scripts where I want to catch every
failure. When I remember to. :-(

        *The return status of a pipeline is the exit status of the last
command,*
*       unless the pipefail option is enabled.  If  pipefail  is  enabled,
 the*
*       pipeline's  return  status is the value of the last (rightmost)
command*
*       to exit with a non-zero status, or zero if all commands  exit
 success‐*
*       fully.*

Two examples, though, are simpler to understand:

# cmp_ok "X"; cmp_ok "OK"
> # echo $?
> 0
>
> # { cmp_ok "X"; cmp_ok "OK"; }
> # echo $?
> 0
>

The shell sees no difference between these and

  $ cmp_ok "X"
  $ cmp_ok "OK"
  $ echo $?

"echo $?" just reports the exit status of the last command.

Semis are just a typing convenience that let you put more than one command
on a line -- exactly like lots of other languages. I've been told that this
feature was first introduced in Algol, by Edgar T. "Ned" Irons, who wanted
to reduce the number of card boxes he had to carry around.

Ned also invented syntax-directed compilation and the screen editor.

If you're really academic, you'll already be thinking, "But in the shell,
is it a statement terminator or a statement separator?" :-)

-- 
Jeffrey Haemer <jeffrey.haemer at gmail.com>
720-837-8908 [cell], http://seejeffrun.blogspot.com [blog],
http://www.youtube.com/user/goyishekop [vlog]
*פרייהייט? דאס איז יאַנג דינען וואָרט.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20130101/4e8019c6/attachment.html>


More information about the LUG mailing list