那么,哪位DX能给我解释解释为什么在经过var=2 | somecmd 之后,var在当前shell里的值没有变?
$ A=B C # 空白鍵未被關掉,作為 IFS 處理。
$ C: command not found.
$ echo $A
$ A="B C" # 空白鍵已被關掉,僅作為空白鍵處理。
$ echo $A
B C
原帖由 blackold 于 2009-6-14 00:16 发表
cmda|cmdb的两个命令确实是分别在不同的subshell中执行。
但PID变了并且PPID为当前shell的PID, 这应该还不足以说明就是subshell中执行的。
比如
$ cmda;cmdb
也满足这个条件。
Each command in a pipeline is executed as a separate process (i.e., in a subshell).
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.
A child shell is also called subshell
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.
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.
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.
A=B echo $A
echo $A
原帖由 lululau 于 2009-6-15 09:46 发表
如果这样说的话,那么
shellPrompt>external_cmd
和
shellPrompt>(external_cmd)
又有什么不同呢
原帖由 lululau 于 2009-6-15 23:02 发表
看了waker和黑哥两位DX的讲解,我好像有点明白了
我画几张图来表达一下我现在的理解
还劳烦各位DX看看我现在的这个理解对不对。
336032
336033
336034
336035
336036
另外我觉得,管道:cmd1 | cmd2 ...
sub shell a child shell executed under an other shell
原帖由 lululau 于 2009-6-15 23:02 发表
看了waker和黑哥两位DX的讲解,我好像有点明白了
我画几张图来表达一下我现在的理解
还劳烦各位DX看看我现在的这个理解对不对。
336032
336033
336034
336035
336036
另外我觉得,管道:cmd1 | cmd2 ...
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |