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

RE: Trap and exit



>
> I am not claiming this is a bug in Zsh, but I confess I prefer the
> behavior from Bash and Ash.
>
> /tmp % cat foo.sh
> nostromo 12:42
> #! /bin/sh
> trap 'echo trap: $?' 0
> exit 59
> /tmp % ash ./foo.sh
> nostromo 12:42
> trap: 59
> /tmp % bash ./foo.sh
> nostromo Err 59
> trap: 59
> /tmp % zsh ./foo.sh
> nostromo Err 59
> trap: 0
> /tmp %
> nostromo Err 59
>
> What do you think?
>

It looks like a bug in Zsh. Quoting Unix 98:

=============
The environment in which the shell executes a trap on EXIT will be identical
to the environment immediately after the last command executed before the trap
on EXIT was taken.
=============

In this case last command was 'exit 59'. What is exit code of _this_ command?
Quoting Unix 98 again:

=============
The exit status will be n, if specified. Otherwise, the value will be the exit
value of the last command executed, or zero if no command was executed. When
exit is executed in a trap action, the last command is considered to be the
command that executed immediately preceding the trap action.
=============

So, on entry into TRAPEXIT function $? is expected to be the same as exit
status. At least, it is how I understand two statements above.

cheers

-andrej



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