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

Re: exit_function - strange behavior

On Mon, Oct 25, 2021 at 4:28 PM Tycho Kirchner <tychokirchner@xxxxxxx> wrote:
> In the function doc for zshexit it states:
>  > Executed at the point where the main shell is about to exit normally.
> This is not called by exiting subshells...

Documentation mistake.  An explicit call to "exit" always triggers the
hook, the reference to "not called by exiting subshells" refers to the
subshell ending by finishing all its commands.

You can avoid that by having the subshell end with "return 123"
instead of "exit 123", except for some reason (possibly a bug) when
the -c option is used in which case return behaves like exit again.

> On the other hand the exit function seems to be not called for the main
> shell, when a subshell called 'exit' immediately before:

That's tricky ... it's because zsh optimizes away one level of shell
when there's nothing else to do after the subshell exits, so the
subshell "becomes" the main shell.

There's definitely a bug in this somewhere because if the last command
executed by the final subshell an anonymous function call:

zsh -fc '...; ( () { return 123 } )'

then the zshexit hook is NEVER called.

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