I've the following script:

set -eE
on_error() { echo ERROR ERROR; }
trap on_error ERR

if [ -n "$(echo $1 |grep a)" ];then
  echo "input contains 'a'"
fi

When executed under bash-4, on_error is evaluated as part of the expression in the 'if'. This does not happen in 3.2:


<bash-3>
amit0 ~ # bash --version
GNU bash, version 4.0.28(2)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
amit0 ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' a
+ set -eE
+ trap on_error ERR
++ grep a
++ echo a
+ '[' -n a ']'
+ echo 'input contains a'
input contains a
amit0 ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+ '[' -n '' ']'
</bash -3>

<bash-4>
cm.stargate ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' a
+ set -eE
+ trap on_error ERR
++ echo a
++ grep a
+ '[' -n a ']'
+ echo 'input contains a'
input contains a
cm.stargate ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+++ on_error
+++ echo ERROR ERROR
+ '[' -n 'ERROR ERROR' ']'
+ echo 'input contains a'
input contains a
</bash-4>

I'm confused: I assumed that the culprit clause complies as "part of the test in an if statement" (man bash) and hence would not trigger an ERR signal.

And then there's the difference between bashes: I couldn't find reference to this in CHANGES.

Would appreciate comments and more references.

10x,
Amit




Reply via email to