0
set -eu
sh -euc 'echo 1; false; echo 2' || echo fail
f() {
    echo 1
    false
    echo 2
}
{ echo 1; false; echo 2; } || echo fail
( echo 1; false; echo 2; ) || echo fail
f || echo fail

Output (at least in sh, ash, bash):

1
fail
1
2
1
2
1
2

Is this the expected behavior? Can you possibly explain this particular case? Why does it work this way? Is sh -c the only workaround?

x-yuri
  • 3,373
  • 1
    Yes, it is the expected behaviour in that it's how POSIX specifies it. You'll find some differences between shells and between versions thereof. One of the many reasons errexit is not recommended except for the simplest of scripts. It's not a proper substitute for doing "proper" error handling. See also http://mywiki.wooledge.org/BashFAQ/105 – Stéphane Chazelas Jan 24 '24 at 11:15

0 Answers0