zsh-workers
 help / color / mirror / code / Atom feed
* Inconsistent behavior of ERR_EXIT with conditionals
@ 2022-11-04 16:37 Philippe Altherr
  2022-11-06 20:45 ` Bart Schaefer
  0 siblings, 1 reply; 21+ messages in thread
From: Philippe Altherr @ 2022-11-04 16:37 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1.1: Type: text/plain, Size: 1854 bytes --]

In the following code, the only difference between fun1() and fun2() is
that in the former the expression "false && true" is within an extra set of
braces but that's enough to make them behave differently. Calling the
fromer doesn't trigger an exit while calling the latter does.

#!/bin/zsh -e
>
> function fun1() {
>     { false && true }
> }
>
> function fun2() {
>     false && true
> }
>
> echo aaa: $?
> false && true
> echo bbb: $?
> fun1
> echo ccc: $?
> fun2
> echo ddd: $?
>

The output of the script (with Zsh 5.8 on Linux and Zsh 5.8.1 on macOS) is
the following:

aaa: 0
> bbb: 1
> ccc: 1
>

The documentation of ERR_EXIT mentions the special case of conditional
expressions. I understand that the command "false" by itself in the
expression "false && true" shouldn't trigger an exit since it appears in a
position where a condition is expected. However, since the expression
"false && true" as a whole evaluates to a non-zero status and doesn't
appear in a position where a condition is expected, I would assume that it
should trigger an exit. Thus, in my opinion the script should have the
following output:

aaa: 0
>

That's obviously not the case. Apparanty Zsh simply ignores non-zero
statuses of conditionals if they weren't generated by the last command.
However that doesn't explain why the call to fun1 doesn't trigger an exit.
Like the call to fun2, it returns with a non-zero status but for some
reason only the latter triggers an exit.

Note that the following functions behave like fun1 and don't trigger an
exit when they are called.

function fun3() {
>     if true; then
>         false && true
>     fi
> }
>
> function fun4() {
>     case foo in
>         * ) false && true;;
>     esac
> }
>

And the following function behaves like fun2 and triggers an exit.


>  function fun5() {

    ( false && true )
> }
>

Philippe

[-- Attachment #1.2: Type: text/html, Size: 3137 bytes --]

[-- Attachment #2: bug.zsh --]
[-- Type: application/octet-stream, Size: 173 bytes --]

#!/bin/zsh -e

function fun1() {
    { false && true }
}

function fun2() {
    false && true
}

echo aaa: $?
false && true
echo bbb: $?
fun1
echo ccc: $?
fun2
echo ddd: $?

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2022-11-11  4:09 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-04 16:37 Inconsistent behavior of ERR_EXIT with conditionals 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
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

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).