zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Philippe Altherr <philippe.altherr@gmail.com>
Cc: zsh-workers@zsh.org
Subject: Re: Inconsistent behavior of ERR_EXIT with conditionals
Date: Mon, 7 Nov 2022 21:36:14 -0800	[thread overview]
Message-ID: <CAH+w=7YCNXmVU8Os29tSr1JrnFpGw47EqRfiJKdXNp0P-3KPGw@mail.gmail.com> (raw)
In-Reply-To: <CAGdYchsaiL-CuU96epSgCtrDqkkJ73mgNkZzY4FSp4-w4qkx+w@mail.gmail.com>

On Mon, Nov 7, 2022 at 8:58 PM Philippe Altherr
<philippe.altherr@gmail.com> 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.


  reply	other threads:[~2022-11-08  5:37 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04 16:37 Philippe Altherr
2022-11-06 20:45 ` Bart Schaefer
2022-11-07  3:50   ` Bart Schaefer
2022-11-07  5:35     ` [PATCH] " Bart Schaefer
2022-11-07  9:44       ` Peter Stephenson
2022-11-08  1:20         ` Bart Schaefer
2022-11-08  4:58     ` Philippe Altherr
2022-11-08  5:36       ` Bart Schaefer [this message]
2022-11-08  8:04         ` Lawrence Velázquez
2022-11-08 18:51           ` Philippe Altherr
2022-11-08 19:20             ` Lawrence Velázquez
2022-11-08 23:28             ` Bart Schaefer
2022-11-09  4:11               ` Philippe Altherr
2022-11-09  6:00                 ` Bart Schaefer
2022-11-09 14:22                   ` Philippe Altherr
2022-11-10  1:00                     ` Bart Schaefer
2022-11-10  5:09                       ` Bart Schaefer
2022-11-11  3:04                         ` Philippe Altherr
2022-11-11  4:06                           ` Lawrence Velázquez
2022-11-11  4:09                           ` Eric Cook
2022-11-08 23:11           ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7YCNXmVU8Os29tSr1JrnFpGw47EqRfiJKdXNp0P-3KPGw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=philippe.altherr@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).