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

Lee Woodworth blug-mail at duboulder.com
Tue Jan 1 12:07:27 MST 2013


On 12/30/2012 12:00 PM, Rob Nagler wrote:

... snip ...

> Not sure how the subshell is detecting this case:
>
> (set -e; { set; env; } | sort>  y)
> (set -e; { set; env; } | sort>  x) || echo ok
> diff x y

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

function cmp_ok {
   test "$1" == "OK"
}

# cmp_ok "X"
# echo $?
1

# cmp_ok "X"; cmp_ok "OK"
# echo $?
0

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

# (exit 5)
#echo $?
5

# cmp_ok "X" | cat >/dev/null
# echo $?
0




More information about the LUG mailing list