[Toybox] Confused by bash trap handler return value.

Rob Landley rob at landley.net
Mon Feb 10 16:25:04 PST 2025


I don't understand how to reconcile the behavior of all three of these 
in debian's bash 5.2.15(1):

$ 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.
2) The previous statement returns 1, the trap handler doesn't run, so 
the next statement sees 1.
3) The previous statement returns 1 and the trap handler returns 1, so 
the next statement sees zero...?

$ kill -s CONT $$ 9876543
bash: kill: (9876543) - No such process
$ echo $?
0

Ah. Bash's builtin kill doesn't return 1 unless it couldn't send ANY 
signals.

$ bash -c $'trap \'echo hello;false\' USR1 && /bin/kill -s USR1 $$ 
9876543; echo here $?'
/bin/kill: (9876543): No such process
hello
here 1
$ bash -c $'trap \'echo hello\' USR1 && /bin/kill -s USR1 $$ 9876543; 
echo here $?'
/bin/kill: (9876543): No such process
hello
here 1

Ok, so bash's trap handler discards the return code and preserves the 
earlier value, and my confusion was that bash's "kill" command returns 0 
for some errors.

Rob


More information about the Toybox mailing list