bash drops errexit option in sourced file
Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc -I/home/abuild/rpmbuild/BUILD/bash-4.2 -L/home/abuild/rpmbuild/BUILD/bash-4.2/../readline-6.2 Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-suse-linux-gnu' -DCONF_VENDOR='suse' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -D_GNU_SOURCE -DRECYCLES_PIDS -Wall -g -std=gnu89 -Wuninitialized -Wextra -Wno-unprototyped-calls -Wno-switch-enum -Wno-unused-variable -Wno-unused-parameter -ftree-loop-linear -pipe -fprofile-use uname output: Linux linux-e1lq 3.4.11-2.16-desktop #1 SMP PREEMPT Wed Sep 26 17:05:00 UTC 2012 (259fc87) x86_64 x86_64 x86_64 GNU/Linux Machine Type: x86_64-suse-linux-gnu Bash Version: 4.2 Patch Level: 24 Release Status: release Description: While normally sourced files have identical options set like the invoking environment this is not true if the sourcing is done from within a function foo that was called with something like: foo && true The following example demonstrates this: $ cat bug #!/bin/bash set -e int() { echo A: $- . source echo D: $- } echo Broken: int && true echo OK: int $ cat source echo B: $- false echo C: $- $ bash bug Broken: A: ehB B: hB C: hB D: ehB OK: A: ehB B: ehB $ This error showed up in bash 4.0 first and was not present in 3.2 or older. As you can see in the invocation "int && true" the flag is still set inside the function but magically disappears once the file is sourced, while in the second incocation "int" everything behaves as expected. Apparently something gets broken when the exit on error is temporarily disabled for the chained command "int && true" because while it gets properly restored within the function itself it disappears again when sourcing the other file. This problem was also reported already as https://savannah.gnu.org/support/index.php?108191 already but Werner Fink suggested to report via bashbug. Repeat-By: Invoke the scripts as described in the details section.
Re: bash drops errexit option in sourced file
On Mon, Dec 3, 2012 at 4:25 PM, Chet Ramey wrote: > Since the `source' command is called in a context where all commands within > it should have the `errexit' flag disabled, bash chooses to satisfy this > requirement by turning off the flag that (internally) represents errexit. > Under most circumstances, however, that state is externally visible as the > value of the -e option. > > Note that the behavior of suppressing the effect of the errexit option is > correct. The value of the -e option in $- should be unaffected, though. Ah, now I start to understand how this is supposed to work. I also realized that it is not possible within the function to turn on the flag again with another internal set -e. Is that also part of the design, meaning there is no way to have an effective set -e within a function in case it was called in that context? Robert