I have a Bash subshell function like this:
f() (
set -e
( exit 1 )
echo "I should not be printed!"
)
If I invoke it like this:
f
I get no output, as expected.
But if I invoke it like this:
f || true
or like this:
if ! f; then : ; fi
I get "I should not be printed!" This is not what I'd expect. My expectation is that, since I'm defining f
using a subshell (f() ( ... )
instead of f() { ... }
), this should be equivalent to putting the contents of f
into its own shell script, then running it as bash ./f.sh
, in which case I get no output.
So, my question is twofold:
Is there a way to get
f
to always return when it hits that( exit 1 )
line, regardless of how it's called, while still keeping it a function? (i.e. without putting the contents off
into its own script and invoking it likebash ./f.sh
?)Is
set -e
not working (a bug in Bash, maybe?), or is this expected behavior? Why does the execution of this function depend on whether it's invoked "bare" (likef
) or as part of an expression that checks the return code (likef || true
)?