Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Inconsistent behavior of ERR_EXIT with conditionals



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: bug.zsh
Description: Binary data



Messages sorted by: Reverse Date, Date, Thread, Author