On 8/8/22 16:38, Robert Elz wrote:
There's no need for anything to make that work, when the trap action
starts running $? is set to whatever it was just previously, and the
code there can save and/or use that value however it sees the need.
Well I'll be. I had to write a script to test it because I was so sure
that at least USR1 and USR2 did not.
Its a shame that for a 'feature' that is not even required, 'return' has
been made quirky so that reusing good code in a trap handler breaks that
code.
FYI: here is script I just used ...
$ cat bin/trapExitCodeTest.sh
#!/usr/bin/env bash
if [ "$1" == "slowerror" ]; then
sleep 0.5
exit 45;
fi
sigToTest="${1:-USR1}"
(
trap 'echo "$sigToTest: last cmd exit = $?"' $sigToTest
bash -c "$0 slowerror"
)&
child=$!
sleep 0.2
kill -$sigToTest $child
sleep 1
$ bin/trapExitCodeTest.sh
USR1: last cmd exit = 45
--BobG