[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
set -e, bad substitutions, and trap EXIT
From: |
Ewan Mellor |
Subject: |
set -e, bad substitutions, and trap EXIT |
Date: |
Tue, 7 Feb 2012 01:18:24 +0000 |
Hi,
Is this a bug? In the script below, I'm using a variable to control whether
the script uses set -e or not, because the behavior is dependent on whether set
-e is set. When the script hits the bad substitution, it runs the exit handler
and then exits as expected. However, with set -e, the exit status for the
script as a whole is wrong - it exits with 0 even though the script is
aborting. That's completely the opposite of what I'd expect. Without set -e
in force, it all works as expected.
I noticed also that when set -e is in force, the exit handler runs in a
different subshell when compared with the other case -- the prefix is "++" vs
"+" in the debug output.
This behavior is independent of whether set -o posix is in force.
~ $ echo $BASH_VERSION
4.2.8(1)-release
~ $ cat ./test.sh
#!/bin/bash
set -x
[ $SET_E ] && set -e
trap echo EXIT
echo ${$NO_SUCH_VAR} # Bad substitution expected here
~ $ SET_E= ./test.sh ; echo $?
+ '[' ']'
+ trap echo EXIT
./test.sh: line 8: ${$NO_SUCH_VAR}: bad substitution
+ echo
1
~ $ SET_E=1 ./test.sh ; echo $?
+ '[' 1 ']'
+ set -e
+ trap echo EXIT
./test.sh: line 8: ${$NO_SUCH_VAR}: bad substitution
++ echo
0
Thanks,
Ewan.
- set -e, bad substitutions, and trap EXIT,
Ewan Mellor <=