幾個知識點
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的