help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

When does bash spawn a sub-shell with ( list ) ?


From: Masahiro Yamada
Subject: When does bash spawn a sub-shell with ( list ) ?
Date: Fri, 28 Jan 2022 04:45:33 +0900

Hi,

The bash manual [1] says
"Placing a list of commands between parentheses causes
a subshell environment to be created"

In this statement, "a list of commands"
means "two commands or more",
excluding a single command.

Is this correct?


I ran a sample program to confirm this.


masahiro@grover:/tmp/test$ cat test.c
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    printf("pid=%d, ppid=%d\n", getpid(), getppid());
}

masahiro@grover:/tmp/test$ gcc test.c
masahiro@grover:/tmp/test$
masahiro@grover:/tmp/test$ echo $$
1071740
masahiro@grover:/tmp/test$ ./a.out
pid=1073015, ppid=1071740
masahiro@grover:/tmp/test$ ( ./a.out )
pid=1073016, ppid=1071740
masahiro@grover:/tmp/test$ ( ./a.out; ./a.out )
pid=1073020, ppid=1073019
pid=1073021, ppid=1073019


The command
    ./a.out
and
    ( ./a.out )
printed the same ppid, 1071740.
So,  ( ./a.out ) did not fork a new subshell.

The command
   (  ./a.out; ./a.out )
printed a different ppid, 173019, which
is a PID of the spawned subshell.


Please let me confirm if I am understanding correctly.




[1]: https://www.gnu.org/software/bash/manual/bash.html

-- 
Best Regards
Masahiro Yamada



reply via email to

[Prev in Thread] Current Thread [Next in Thread]