几个知识点
1.Bash在实现pipeline(管道|)时会发起两个subshell(子shell)来运行|两边的命令,对于系统来说就是发起两个childprocess(子进程)

2.fork是产生process的唯一途径,exec*是执行程序的唯一途径

3.子进程会完全复制父进程,除了$PID与$PPID

4.fork子进程时继承父进程的进程名,在exec*执行命令时才由exec*替换为子进程对应的命令,同一进程的命令名可以由一个个exec*任意多次的改变



[注]对于linux平台,JB上就是这样的,其它平台不好发表意见,当然对于2中的两个唯一有一个例外,就是在kenerl  init的初期;
暂时找不到相关参考,也没有功力读源码,所以此论是道听途说级别,错误之处请指出改正,如果没有改正的价值可一笑而过

我觉得要先弄清楚sub shell的定义。

查了些资料,发现subshell的定义有些混乱。
bashMan:



QUOTE:
Each command in a pipeline is executed as a separate process (i.e., in a subshell).  




QUOTE:
When a simple command other than a builtin or shell function is to be executed, it is invoked in a
separate execution environment that consists of the following. Unless otherwise noted, the values are
inherited from the shell.



这个separate execution是subshell吗?
A. 在当前shell执行外部命令,如shell> date, 是fork+exec, 算不是subshell? 
B. ()是fork一个child shell,该child再fork+exec来执行命令。这个subshell和A中的"subshell"显然是不同的。

UNIX: The Textbook, by Syed Mansoor Sarvar, Robert Koretsky and Syed Aqeel Sarvar中提到:


QUOTE:
A child shell is also called subshell


问题是fork+exec是fork一个child shell,然后在该child shell中exec.
而执行脚本(shell>scriptname)时,是fort一个child shell A,该child shell A再fork一个child shell B, 在B中再exec.

那么child shell是指哪种情况?



UNIX at Fermilab中的Important UNIX Concepts:



QUOTE:
When you give the shell a command associated with a compiled executable or shell script, the shell
creates, or forks, a new process called a subshell.


外部命令也在subshell中执行。



QUOTE:
To execute most built-in commands, the shell forks a subshell which executes the command directly (no
exec system call). For the built-in commands cd, set, alias and source, the current shell executes the
command; no subshell is forked.



shell> built-inCommands这样执行时,大部分内部命令也是在subshell中执行。
可见,UNIX at Fermilab认为fork 一个child shell就是subshell, 不管是fork-fork+exec, 还是 fork+exec。

我刚才又去翻了下ABS,定义与我的理解不一样


QUOTE:
A subshell is a separate instance of the command processor -- the shell that gives you the prompt at the
console or in an xterm window. Just as your commands are interpreted at the command line prompt, similarly
does a script batch-process a list of commands. Each shell script running is, in effect, a subprocess (child
process) of the parent shell.
./script也是external_cmd的一种啊,都是fork-exec,至于external-cmd再作什么动作完全是external-cmd的