What's the logic in exiting early on failure in blocks and subshells in Bash?
1
vote
0
answers
71
views
In Bash blocks {} and subshells () the exit early doesn't work if there is an OR condition following it. Take for example
set -e
{ echo a; false; echo b; } || echo c
prints
a
b
and
set -e
{ echo a; false; echo b; false;} || echo c
prints
a
b
c
It seems to take the last executed command's exit code only. While this makes sense, given a semicolon instead of &&
is used, I'd expect the set -e
to still make it exit on the first false
and execute the error handling echo c
code.
Using &&
instead of ;
does make it work, but that makes it messy when having multiple line blocks. Adding in set -e
at the start of the block/subshell also has no effect.
The reason this confuses me is because
set -e
{ echo a; false; echo b; }
prints
a
which means the exit-on-failure works when there's no ||
code following it. So I'd expect this to be the case with ||
code following it, executing it after the first failure in the block. Is there no way to achieve that without appending &&
after each line in the block?
Asked by QuickishFM
(141 rep)
Dec 20, 2024, 01:34 PM
Last activity: Dec 20, 2024, 02:06 PM
Last activity: Dec 20, 2024, 02:06 PM