LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2468|回复: 20

shell中的小括号()问题

[复制链接]
发表于 2009-2-23 12:11:24 | 显示全部楼层 |阅读模式
现在正学习shell编程,遇见了一个问题,特请教高手解答,谢谢!

shell中的小括号()可以把命令放在子shell中执行,如下:

$var=hello
$(var=hellotest; echo $var)
$hellotest
$echo $var
$hello

说明在子shell中的局部环境变量对父shell不影响

现在运行
$var=hello
$(echo $var) // 子shell只能继承export导出的变量,var=hello是局部变量不会被子shell继承!这里是在子shell中执行的吧?!,下面就不应该显示hello
$hello
$
怎么没有在shell中给var赋值就引用了父shell中的变量了呢?
按照前面的说法:()小括号中的命令在子shell中执行,那么应该是下面才对啊
$var=hello
$(echo $var)            
$
$echo $var
$hello

谁解释下,谢谢了
发表于 2009-2-23 13:38:47 | 显示全部楼层
对于这个问题我的理解是:父 shell 只能继承子 shell 用 export 导出的变量,而子 shell 可以继承父 shell 的所有变量。
回复 支持 反对

使用道具 举报

发表于 2009-2-23 23:14:11 | 显示全部楼层
用你的话讲,就是说当子shell中未定义局部变量时他将引用父shell已定义的变量,而不会是空值
回复 支持 反对

使用道具 举报

发表于 2009-2-25 21:21:28 | 显示全部楼层
楼上两位哥儿的回复挺令人汗颜的啊,尤其是头一遭听闻这个...
Post by poet;1951416
对于这个问题我的理解是:[color="Blue"]父 shell 只能继承子 shell 用 export 导出的变量,而子 shell 可以继承父 shell 的所有变量。
完全错误!

shell确实有很多看似不符合常理的情况,但是manpage里全部交代清楚了!
  1.        [color="Red"]Command  substitution, commands grouped with parentheses, and asynchro-
  2.        nous commands are invoked in a subshell environment that is a duplicate
  3.        of  the  shell  environment,  except that traps caught by the shell are
  4.        reset to the values that the shell inherited from its parent at invoca-
  5.        tion.[/color]  Builtin commands that are invoked as part of a pipeline are also
  6.        executed in a subshell environment.  Changes made to the subshell envi-
  7.        ronment cannot affect the shell's execution environment.
复制代码
shell  environment 里到底包括了些什么?往上翻阅manpage!
  1.        The shell has an execution environment, which consists of  the  follow-
  2.        ing:
  3.        o      open  files inherited by the shell at invocation, as modified by
  4.               redirections supplied to the exec builtin
  5.        o      the current working directory as set by cd, pushd, or  popd,  or
  6.               inherited by the shell at invocation
  7.        o      the  file  creation  mode mask as set by umask or inherited from
  8.               the shell's parent
  9.        o      current traps set by trap
  10. [color="Red"]       o      shell parameters that are set by variable assignment or with set
  11.               or inherited from the shell's parent in the environment[/color]
  12.        o      shell  functions  defined during execution or inherited from the
  13.               shell's parent in the environment
  14.        o      options enabled at invocation (either by default  or  with  com-
  15.               mand-line arguments) or by set
  16.        o      options enabled by shopt
  17.        o      shell aliases defined with alias
  18.        o      various  process  IDs,  including  those of background jobs, the
  19.               value of $$, and the value of $PPID
复制代码
至少在楼主提问之前我不晓得怎么回答这个问题,manpage给了我最好的答案。谢谢楼主!谢谢manpage!
回复 支持 反对

使用道具 举报

发表于 2009-2-26 20:48:33 | 显示全部楼层
Post by Jockey;1952615
楼上两位哥儿的回复挺令人汗颜的啊,尤其是头一遭听闻这个...

完全错误!

shell确实有很多看似不符合常理的情况,但是manpage里全部交代清楚了!
  1.        [color="Red"]Command  substitution, commands grouped with parentheses, and asynchro-
  2.        nous commands are invoked in a subshell environment that is a duplicate
  3.        of  the  shell  environment,  except that traps caught by the shell are
  4.        reset to the values that the shell inherited from its parent at invoca-
  5.        tion.[/color]  Builtin commands that are invoked as part of a pipeline are also
  6.        executed in a subshell environment.  Changes made to the subshell envi-
  7.        ronment cannot affect the shell's execution environment.
复制代码

shell  environment 里到底包括了些什么?往上翻阅manpage!
  1.        The shell has an execution environment, which consists of  the  follow-
  2.        ing:

  3.        o      open  files inherited by the shell at invocation, as modified by
  4.               redirections supplied to the exec builtin

  5.        o      the current working directory as set by cd, pushd, or  popd,  or
  6.               inherited by the shell at invocation

  7.        o      the  file  creation  mode mask as set by umask or inherited from
  8.               the shell's parent

  9.        o      current traps set by trap

  10. [color="Red"]       o      shell parameters that are set by variable assignment or with set
  11.               or inherited from the shell's parent in the environment[/color]

  12.        o      shell  functions  defined during execution or inherited from the
  13.               shell's parent in the environment

  14.        o      options enabled at invocation (either by default  or  with  com-
  15.               mand-line arguments) or by set

  16.        o      options enabled by shopt

  17.        o      shell aliases defined with alias

  18.        o      various  process  IDs,  including  those of background jobs, the
  19.               value of $$, and the value of $PPID
复制代码

至少在楼主提问之前我不晓得怎么回答这个问题,manpage给了我最好的答案。谢谢楼主!谢谢manpage!


说实话 看不懂
回复 支持 反对

使用道具 举报

发表于 2009-2-26 23:30:51 | 显示全部楼层
通过变量赋值的shell参数会成为shell环境的一部分,而通过圆括号创建的子shell所处的环境是父shell环境的一个副本。

也就是说,圆括号创建的子shell,与你直接 bash -c 'do some thing' 创建的子shell环境确实是有些不同的!
回复 支持 反对

使用道具 举报

发表于 2009-2-27 02:00:42 | 显示全部楼层
哦,楼上的意思是子shell的建立完全继承了父shell的环境,
但是对子shell环境的改变并不会反过来影响父shell,
是吗?
回复 支持 反对

使用道具 举报

发表于 2009-2-27 10:08:10 | 显示全部楼层
Post by realasking;1953370
哦,楼上的意思是子shell的建立完全继承了父shell的环境,
但是对子shell环境的改变并不会反过来影响父shell,
是吗?


E文太差,看不懂,版主给个明示吧,是否这里所说的意思
回复 支持 反对

使用道具 举报

发表于 2009-2-27 10:58:23 | 显示全部楼层
Post by realasking;1953370
[color="Red"]哦,楼上的意思是子shell的建立完全继承了父shell的环境,
[color="Blue"]但是对子shell环境的改变并不会反过来影响父shell,
是吗?

红色字不准确,蓝色字是对的。
(do something) 创建的是子shell;
bash -c 'do something' 创建的也是子shell;
./script.sh 创建的也是子shell;etc。
但是完全复制了父shell环境的就三种情况:
  • command  substitution,命令替换,也就是$(do something)
  • commands grouped with parentheses,圆括号中的命令组,也就是(do something)
  • asynchronous commands,异步命令,这个我貌似还没遭遇过... XD
回复 支持 反对

使用道具 举报

发表于 2009-2-28 21:04:54 | 显示全部楼层
嗯,受教了。
不过楼上能否举几个创建子shell但没有复制父shell的环境的例子?
这个确实一直没有弄清楚。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表