Why does the shell command "{command1; command2:} &" open a subshell?

As we all know, placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. But when using "&" after "{}", why are two subshells created? pid 1002 and 1003.

{
    ./a.out
} &

sleep 19

enter image description here

when using "./a.out &" only a subshell is created. pid 17358.

./a.out &
sleep 19

enter image description here

Why?

+4
source share
2 answers

, - . , ; . bash , . , , .

, , , , :

$ {
> sleep 1; sleep 2; sleep 3; sleep 4; sleep 5
> } &
$ disown
$ ps -f | grep sleep
dave     31845 31842  0 03:50 pts/1    00:00:00 sleep 3 
dave     31849 31771  0 03:50 pts/1    00:00:00 grep sleep

, .

, , .

bash, PID 1002, script, . (, ) -; script bash.

+4

, ( , {...}) ( ) .

, - 0.

C fork(), execvp().


: . .

:

./a.out &

BASH a.out , a.out .

:

{ ./a.out; } &

BASH fork , {...}, a.out . , , BASH 2 . 2- pid, , a.out.

+4

All Articles