bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

set -e, braces and compound commands


From: Daniel Villeneuve
Subject: set -e, braces and compound commands
Date: Thu, 10 Feb 2011 23:08:37 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101208 Red Hat/3.1.7-3.el6_0 Lightning/1.0b2 Thunderbird/3.1.7

I'm using GNU bash, version 4.1.2(1)-release (i386-redhat-linux-gnu).

Following the change of semantics of "set -e" in bash 4,"man bash" seems to imply that the following script should exit after the for command (from the text "or one of the commands executed as part of a command list enclosed by braces" where "one of the commands" should match a "for" command) . However it does not.


set -e
{
  for i in a b c; do
    [ -z "$i" ] && echo "null"
  done
  echo "'for' return code = $?"
}
echo "brace return code = $?"


For reference, I've consulted
http://thread.gmane.org/gmane.comp.standards.posix.austin.general/282, http://thread.gmane.org/gmane.comp.shells.bash.bugs/13465 and
the current public POSIX spec I have access to which still says:

*-e*
   When this option is on, if a simple command fails for any of the
   reasons listed in Consequences of Shell Errors
   <ref to V3_chap02.html#tag_18_08_01>
   or returns an exit status value >0, and is not part of the compound
   list following a *while*, *until*, or *if* keyword, and is not a
   part of an AND or OR list, and is not a pipeline preceded by the *!*
   reserved word, then the shell shall immediately exit.


Question 1: Is it the case that the bash 4 behavior is as intended here and that it's the documention of "set -e" in bash 4 that is imprecise ("commands" being too broad)?

Also, note that the man page section on "trap ERR" says that the trap is triggered if a "simple command" returns >0 (sharing exceptions with "set -e"). It seems however that the trigger is the same as for "set -e", as in

trap 'echo "error caught ($?), exiting"; exit 1' ERR
(exit 2)
echo done

Question 2: Are "set -e" and "trap ... ERR" triggered by the same events?

Clues would be appreciated.
--
Daniel Villeneuve
AD OPT, a Kronos Division




reply via email to

[Prev in Thread] Current Thread [Next in Thread]