LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: johnny_jiang

实在忍不住了,一直想不通的问题, backslash在$() 和 ``中的不同

[复制链接]
 楼主| 发表于 2006-3-19 11:19:20 | 显示全部楼层
Post by yongjian
subshell inherits everything from parent shell but not the other way, unless you use export..
  1. a=1;echo `echo $a`
复制代码
an easy sample. For using en... I was able to type ch but several weeks ago, I upgraded to dapper, and firefox just crashes every time when I use it. I switched to opera but it doesn't support ch input... have no choice right now but waiting for a stable firefox update.


那么yongjian兄,我可不可以把subshell理解为subprocess呢。
如果我的理解没错的话,subprocess应该只能继承被export过的变量吧?

期待中...
回复 支持 反对

使用道具 举报

发表于 2006-3-19 15:30:45 | 显示全部楼层

  1. #!/bin/bash

  2. a=10
  3. echo $a


  4. (
  5. echo "in subshell"
  6. echo $a
  7. )
复制代码

Here is how I tested it. You know cmds within () creates subshell right...
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-19 22:48:18 | 显示全部楼层
多谢各位一直以来的帮助,小弟参考了Learning the bash Shell - 2nd Edition (o'reilly),在仔细的看了bash manual,发现了一些信息,我希望对大家有用

The most important things you need to know about subshells are what characteristics they get, or inherit, from their parents. These are as follows:

·         The current directory

·         Environment variables

·         Standard input, output, and error, plus any other open file descriptors

·         Signals that are ignored

Just as important are the things that a subshell does not inherit from its parent:

·         Shell variables, except environment variables and those defined in the environment file (usually .bashrc)

·         Handling of signals that are not ignored

Code inside a nested subshell obeys the above rules of subshell inheritance, except that it knows about variables defined in the surrounding shell.


在bash manual中
$:   Expands to the process ID of the shell. In a ( ) subshell, it expands to the process ID of the current shell, not the subshell.


从以上的信息可以看出nested subshell和脚本引用的subshell是有区别的
1.nested subshell除了可以继承环境变量,它对shell的其他变量也继承了,但是用脚本引用的subshell不会
2.(echo $$)将回显的是current shell的进程号

小弟个人意见,仅供参考,欢迎砸帖,呵呵!
回复 支持 反对

使用道具 举报

发表于 2006-3-20 03:07:46 | 显示全部楼层
ya, if you do like a=1 and then write a script to echo $a, you get nothing. I am thinking one is using child processing, and the other is threading...
回复 支持 反对

使用道具 举报

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

本版积分规则

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