1

When I do

( sleep 1; read x ; echo x=$x; echo done ) & 

then with the default terminal settings, the job gets stopped by SIGTTIN.

If I do

( ( sleep 1; read x ; echo x=$x; echo done ) & )

the read syscall inside read gets EOF (returns with 0)` and no stopping by SITTIN happens.

What is the explanation for these behavior

Petr Skocik
  • 28,816

1 Answers1

2

That's because in the second case the backgrounded command will be run in a subshell, and as there's no job control in subshells, the background mode will be faked by redirecting the input from /dev/null and ignoring the SIGINT and SIGQUIT signals.

See also these answers:

Background process of subshell strange behaviour

Process started by script does not receive SIGINT

Does ` (sleep 123 &)` remove the process group from bash's job control?

Process killed before being launched in background