Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Inconsistent behavior of ERR_EXIT with conditionals
On Mon, Nov 7, 2022 at 8:58 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> My understanding of ERR_EXIT is that it should trigger an exit of the shell immediately after any command returns with a non-zero exit status, with the exception of commands whose exit status stands for a condition (for example in the condition of an if statement or on the left of an || operator). In other words, when ERR_EXIT is enabled, any command that unexpectedly fails should trigger an immediate exit of the shell.
The key word is "unexpectedly".
The output of bash 5.0 for your script (with minor syntax differences tweaked):
Success-A
Failure-a
Success-B
Failure-b
Failure-c
Failure-d
Failure-e
Failure-f
So you can see that zsh agrees with bash on your "Failure" cases. The
output of dash (as /bin/sh on ubuntu) is the same as zsh with the
exception of Failure-b, which can't be tested because there is no
equivalent of "declare" (or "local"). Bash disagrees with dash and
zsh on your "Success" cases, at least at that version.
The assumption is that anything between the keywords "if" and "then"
(or between "while" and do" by the way) is a compound expression whose
final result needs to be reached. The entire expression on either
side of an && (or || ) is treated the same way, and the use of ! as
"not" is similar -- in all of those cases, the fact that the
programmer is explicitly testing an exit status is interpreted to mean
that a failure is "expected". ERR_EXIT kicks in only when the result
is not otherwise checked.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author