The following Bash loop stops if I interrupt it with ^C
:
(while true; do sleep 5; done)
The following, however, does not:
(while true; do aplay test.wav; done)
The difference, from what I can tell, is that aplay
catches and handles SIGINT
, whereas sleep
does not.
Why is this? Does POSIX specify this behavior somewhere (I notice that Dash does the same thing), and if so, why? I cannot quite understand why this behavior is desirable. In fact, how can Bash even tell the difference? I must admit I'm not aware of any mechanism (other than ptrace
or /proc/
hacking, at least) by which one process can tell how another one handles signals.
Also, is there a way to counteract it? I notice that not even a trap 'exit 0' SIGINT
before the loop helps.
(EDIT: Running the loops in a subshell is important, because otherwise the parent shell does not receive the SIGINT
.)