Discussion:
[Help-bash] Difference for SIGINT between subshells and external programs?
Peng Yu
2018-12-07 04:22:50 UTC
Permalink
Hi,

Non-builtin commands run by bash have signal handlers set to the values
inherited by the shell from its parent. When job control is not in
effect, asynchronous commands ignore SIGINT and SIGQUIT in addition to
these inherited handlers. Commands run as a result of command substi-
tution ignore the keyboard-generated job control signals SIGTTIN, SIGT-
TOU, and SIGTSTP.

I am trying to understand what "asynchronous commands" refer to in the
above text. Does it just refer to external commands run with "&" at
the end, and subshell is not included?

When I type ctrl-C after main.sh runs, the sleep processes still keep
running. So it matches the manual description.

ctrl-C triggers the traps in the subshells in main_subshell.sh. So
"asynchronous commands" do not apply to them? These subshells still
receive the SIGINT signals?

Thanks.

$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

for i in {1..3}
do
sleep 100 &
done
wait
$ ./main.sh
^C
$ ps -t "$(tty | cut -f 3 -d /)" |grep sleep
98683 ttys059 0:00.01 sleep 100
98684 ttys059 0:00.00 sleep 100
98685 ttys059 0:00.00 sleep 100
98699 ttys059 0:00.01 grep --color sleep
$ cat main_subshell.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

for i in {1..3}
do
(
trap 'echo SIGINT $BASHPID' SIGINT
sleep 100
) &
done
wait
$ ./main_subshell.sh
^CSIGINT 99371
SIGINT 99369
SIGINT 99370
--
Regards,
Peng
Chet Ramey
2018-12-07 14:47:10 UTC
Permalink
Post by Peng Yu
I am trying to understand what "asynchronous commands" refer to in the
above text. Does it just refer to external commands run with "&" at
the end, and subshell is not included?
Asynchronous means commands for which the shell does not wait to complete
before proceding with the next command. It's captured in the grammar as
`asynchronous lists':

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_03_02

It's commands terminated with `&' and, in bash, coprocesses and process
substitution.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
Loading...