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

Subshell return codes are discarded during logout?



Hello,

It seems that the return/error codes of subshells are ignored while the shell is shutting down. This happens whether the exit builtin is used or the shell is exiting due to a signal. This can't be intentional? I saw no mention of such an edge-case in the documentation.

For example:

% cat .zlogout
(echo ran; return 1)
echo $?
% source .zlogout
ran
1
% exit
ran
0

I was able to reproduce this on all of my machines, all running some iteration of zsh 5.9, including:
- Alpine Linux (musl libc), x86
- Android, armv8a/aarch64, under Termux
- Arch Linux, x86-64
- OpenWrt, armv8a/aarch64

---

For some background as to how I stumbled across this, since it may showcase a situation where this bug could cause seriously unexpected functionality:

Somewhat related to the recent drama about short loops and alternative forms, is the interesting concept of surrounding non-braced command executions in conditionals with braces of some kind in order to use the alternative or short syntax. Personally, I do this all the time as I prefer this syntax to traditional shell conditionals and loops, which I find quite clunky and error-prone. (Yes, I sense the irony.)

For example:

if kill -0 123; then
    echo hello
fi

becomes, with alternate form:

if {kill -0 123} {
    echo hello
}

or with alternate form and short loops:

if {kill -0 123} echo hello

With this syntax beibg valid, it may be expected (erroneously) that the following is precisely equivalent, with the parenthesis acting as grouping syntax only:

if (kill -0 123) {
    echo hello
}

To developers of various languages that require such syntax for their conditional statements, this may appear to be perfectly logical. However, the more eagle-eyed will notice immediately the difference between this version and the former: The kill command is run inside a subshell.

It is equivalent instead to this:

if (kill -0 123); then
    echo hello
fi

In most basic use-cases, this difference should affect nothing, since subshells do not operate much differently ftom normal code execution. But during logout, while the subshell is executed like normal, the if return value is ignored, and the if statement will execute as if the condition succeeded.

Best regards.


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