Bash script doesn't see SIGHUP?
The Bash manual states:
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.
That means that despite the signal is received by bash
when you send it, your trap on SIGHUP will be called only when cat
ends.
If this behavior is undesirable, then either use bash
builtins (e.g. read
+ printf
in a loop instead of cat
) or use background jobs (see Stéphane's answer).
@xhienne has already explained why, but if you wanted to have the signal acted on straight away (and not exit the script), you could change your code to:
#! /bin/bash -
interrupted=true
trap 'interrupted=true; echo HUP' HUP
{ cat <&3 3<&- & pid=$!; } 3<&0
while
wait "$pid"
ret=$?
"$interrupted"
do
interrupted=false
done
exit "$ret"
The little dance with file descriptors is to work around the fact that bash
redirects stdin to /dev/null
for commands launched in background.