[Toybox] Confused by bash trap handler return value.
Chet Ramey
chet.ramey at case.edu
Tue Feb 11 06:34:42 PST 2025
On 2/10/25 7:25 PM, Rob Landley wrote:
> I don't understand how to reconcile the behavior of all three of these in
> debian's bash 5.2.15(1):
Trap actions have no "return value." POSIX says:
"The value of "$?" after the trap action completes shall be the value it
had before the trap action was executed."
>
> $ bash -c $'trap \'echo hello;false\' USR1 && kill -s USR1 $$ && echo here $?'
> hello
> here 0
> $ bash -c $'trap \'echo hello;false\' USR1 && kill -s USR1 9876543; echo
> here $?'
> bash: line 1: kill: (9876543) - No such process
> here 1
> $ bash -c $'trap \'echo hello;false\' USR1 && kill -s USR1 $$ 9876543; echo
> here $?'
> bash: line 1: kill: (9876543) - No such process
> hello
> here 0
>
> 1) The previous statement returns 0 but the trap handler returns 1, so the
> next statement sees zero.
Executing a trap action doesn't change $?.
> 2) The previous statement returns 1, the trap handler doesn't run, so the
> next statement sees 1.
As if the trap were not there at all.
> 3) The previous statement returns 1 and the trap handler returns 1, so the
> next statement sees zero...?
The AND-OR list returns 0, since the trap command succeeds, the kill
command runs, and kill returns 0 if it sends at least one signal
successfully. That is documented:
"kill returns true if at least one signal was successfully sent,
or false if an error occurs or an invalid option is encountered."
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet at case.edu http://tiswww.cwru.edu/~chet/
More information about the Toybox
mailing list