Unix & Linux
bash shell process posix subshell
Updated Fri, 20 May 2022 05:00:48 GMT

Is a sub-shell the same thing as a child-shell


There are this two names: a subshell and a child-shell.

Yes, a child process will be started by any of this:

sh -c 'echo "Hello"'
( echo "hello" )
echo "$(echo "hello")
echo "hello" | cat

Are all equivalent and share the same name? Do all share the same properties?


POSIX has this definition:

A shell execution environment consists of ....

But the last paragraph of above link has this:

A subshell environment shall be created as a duplicate of the shell environment, except that signal traps that are not being ignored shall be set to the default action.

And specially:

Command substitution, commands that are grouped with parentheses, and asynchronous lists shall be executed in a subshell environment. Additionally, each command of a multi-command pipeline is in a subshell environment; ....

The sh -c 'echo "Hello"' is not included there, should that be called a subshell also?




Solution

A subshell duplicates the existing shell. It has the same variables, the same functions, the same options, etc. Under the hood, a subshell is created with the fork system call; the child process goes on to do what is expected of it while the parent waits (e.g., $()) or goes on with its life (e.g., &) or otherwise does what is expected of it (e.g., | ).

sh -c does not create a subshell. It launches another program. That program happens to be a shell, but that's just a coincidence. The program may even be a different shell (e.g., if you run sh-c from bash, and sh is dash), i.e., a completely different program that just happens to have significant similarities in its behavior. Under the hood, launching an external command (sh or any other) calls the fork system call and then the execve system call to replace the shell program in the subprocess by another program (here sh).

Including $$, but excluding some shell-specific variables such as bash and mksh's BASHPID.
At least, that's the traditional and usual implementation. Shells can optimize the fork away if they can mimic the behavior otherwise. See What is the exact difference between a "subshell" and a "child process"?

Relevant man pages: fork(2), execve(2).





Comments (5)

  • +0 – Thanks. What does running a bash script with bash shebang belong to? — Feb 12, 2016 at 00:44  
  • +0 – @Tim Same thing as sh -c: that's a subprocess that coincidentally happens to be a shell. — Feb 12, 2016 at 00:53  
  • +0 – (1) " launching an external command (sh or any other) calls the fork system call and then the execve system call to replace the shell program in the subprocess by another program (here sh)." Is it right that fork first creates a subshell and then execve replace the subshell with the external program? So in the two cases in your reply, a subshell is always created? (2) "A subshell that duplicates the existing shell." what does "that" mean? — Feb 12, 2016 at 01:32  
  • +0 – (3) in bash -c <command>, after fork the shell and then execve bash -c <command> , a bash shell is created. Then are the system call used to run <command> again fork the bash shell and execve <command>? — Feb 12, 2016 at 01:42  
  • +2 – @cuonglm Child shell is not a technical term with a specific meaning, unlike subshell. If it's a shell that's a child, then it's a child shell, following the usual rules of English. — Feb 12, 2016 at 12:36