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