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

BUG: "return 0 && x=abc" does not skip parameter assignment



Dear Zsh contributors,

The Z Shell Manual says in "17 Shell Builtin Commands":

    return [ n ]
        Causes a shell function or ‘.’ script to return to the invoking script
        ...

In fact, the following scripts behave in that way, returning from the
functions without executing subsequent commands:

    $ zsh -c 'f() { return 0 && echo "This is not printed."; }; f'
    $ zsh -c 'f() { return 1 || echo "This is not printed."; }; f'

However, it does not behave that way when a parameter assignment follows the
`return` command within a sublist, as shown below:

    $ zsh -c 'f() { return 0 && x=abc; }; f; echo "$x"'
    abc
    $ zsh -c 'f() { return 1 || x=abc; }; f; echo "$x"'
    abc

I expected the function to return without performing the parameter assignment,
but the assignment was actually executed.

I am not sure if this will be helpful, but here is an example that shows what
is happening in a little more detail:

    $ zsh -c '
        f() {
            return 0 && x=abc && echo "This is not printed." && y=def
            echo "This is not printed."
        }
        set -x
        f
        echo "$x" "$y"
    '
    +zsh:7> f
    +f:1> return 0
    +f:1> x=abc
    +f:1> y=def
    +zsh:8> echo abc def
    abc def

The last `echo` command in the function `f` is not executed, so `return 0`
seems to have effect. However, parameter assignments are also executed before
the function returns by the `return` command.

I observed these behaviors in zsh 5.9 on Debian and current git master (zsh
5.9.999.3-test).

Kind regards,
Takaaki





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