From Bash manual
If Bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.
When Bash is waiting for an asynchronous command via the wait builtin, the reception of a signal for which a trap has been set will cause the wait builtin to return immediately with an exit status greater than 128, immediately after which the trap is executed.
I created some experiments for the first sentence.
In a shell running in a tab in lxterminal:
$ trap "echo hello" HUP $ sleep 100
I then close the tab window by clicking "x" icon, so that send SIGHUP to the shell.
The shell exits immediately, instead of waiting for
sleep 100to complete, and then executing the trap. I was wondering why? Did I miss something?
This one works
$ trap -p trap -- 'echo hello' SIGHUP $ echo $$ 19708 $ sleep 100
Then run from another tab of lxterminal:
$ kill -HUP 19708 $
When the sleep command in the first tab completes:
$ sleep 100 hello