I just recently learned that "subshell" is not the same as "child shell process" (see for example What is the exact difference between a "subshell" and a "child process"? and the POSIX definitions of subshell and child process).
To convince myself of this, I am looking for a command that illustrates (proves) that a subshell is created without a child-shell being spawned.
For now, everything I tried seemed to spawn a child-shell whenever a subshell is created:
$ echo $BASHPID; (pwd; cd ..; echo $BASHPID; pwd); pwd # `( ...)` executed in a subshell
# and in a child-shell process
$ >&2 ps | ps # Theoretically executed in two subshells and apparently without child-shells
# but I cannot be sure due to the outcome of the next example
$ $ >&2 echo $BASHPID | ps # `ps` doesn't display a child-shell for the execution of `echo`
953790 # but `echo $BASHPID` shows a new process that is necessarily
PID TTY TIME CMD # a child-shell since echo is a built-in
948538 pts/2 00:00:00 bash
953791 pts/2 00:00:00 ps
I am looking for a way to demonstrate that having a subshell doesn't necessarily imply having a child-shell...
Bash 5.0.17
a=3; (a=12); echo $a
. The reverse is also true: at least on Linux, one could implement multiple processes within a single subshell environment (i.e.cd
could be an external command, running in a separate process, and still change the cwd of its parent). But AFAIK no mainstream shell is making use of that.