LinuxSir.cn,穿越时空的Linuxsir!

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

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

[复制链接]
发表于 2006-3-17 23:29:50 | 显示全部楼层
Post by johnny_jiang
But why $x is substituted for its value 1, I think '$' should be escaped by '\' in ``.

no, a single "\" in `` means nothing. it will get rid of it first and then bring the rest to subshell. if you want to get $x, you need to do
  1. echo `echo \\$x`
复制代码
, so that `` will treat it as its literal value "\" and bring it to the subshell, in that case, bash will see "echo \$x", which is the same as $(echo \$x). In bash echo, a single "\" will be treated literally, so it can esc "$".
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-18 00:21:18 | 显示全部楼层
Post by yongjian
no, a single "\" in `` means nothing. it will get rid of it first and then bring the rest to subshell. if you want to get $x, you need to do
  1. echo `echo \\$x`
复制代码
, so that `` will treat it as its literal value "\" and bring it to the subshell, in that case, bash will see "echo \$x", which is the same as $(echo \$x). In bash echo, a single "\" will be treated literally, so it can esc "$".


yongjian兄:

鉴于怕产生理解上的错误,我还是用中文表达吧。
no, a single "\" in `` means nothing.

以上这句是指\将不被当前shell作为metacharacter处理对吗,所以不逃逸后面的$?

In bash echo, a single "\" will be treated literally, so it can esc "$".

对上面这句不理解,单个\被最为普通字符处理,而又可以逃逸$?

这样的话,问题就有两个:
1。如果命令替换中存在backslash,它是在子shell中被处理的吗,会对其后紧跟的特殊字符(例如$) 产生逃逸作用呢?
2。根据大家的总结,在``中\被特殊处理,而在$()中\被认为普通字符

忘yongjian兄不惜赐教。小弟是个刨根问底的人,麻烦了!!!:thank
回复 支持 反对

使用道具 举报

发表于 2006-3-18 12:40:05 | 显示全部楼层
simply, in ``, "\" is a metadata, so `` will process it first before the cmd gets sent to subshell. In order to let subshell see one "\", you will have to have "\\" in ``. A single "\" in `` will simply be ignored. Where in $(), the situation is different, $() treats "\" literally, so it sends whatever in $() to the subshell, so only one "\" is enough.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-18 15:49:59 | 显示全部楼层
To yongjian:

From your statement, does it mean `` will treat \ in current shell before it gets sent to the subshell?
If it is, when the parameter substitution happens?
I mean


  1. bash# x=1
  2. bash# echo `echo \$x`
  3. bash# 1
复制代码


When is $x replaced, or rather, where is $x replaced? In current shell or subshell?

I always take it that $x is replaced in current shell and before command substitution because parameter expansion is ahead of command substitution.

So I'm confued why $x is not influenced by the preceding \.
回复 支持 反对

使用道具 举报

发表于 2006-3-18 17:52:39 | 显示全部楼层
I don't think bash or any other shells will take apart the `` or $(), instead, it will do the cmd sub first and take whatever inside to subshell. Since $x is inside ``, so it shouldn't be processed before the cmd sub but after, which is actually in subshell.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-18 19:12:16 | 显示全部楼层
Post by yongjian
I don't think bash or any other shells will take apart the `` or $(), instead, it will do the cmd sub first and take whatever inside to subshell. Since $x is inside ``, so it shouldn't be processed before the cmd sub but after, which is actually in subshell.


But $x is not an exported variable, that is, it is not an environmental variable.

Subshell will inherit it?
回复 支持 反对

使用道具 举报

发表于 2006-3-18 19:20:46 | 显示全部楼层
Post by johnny_jiang
But $x is not an exported variable, that is, it is not an environmental variable.

Subshell will inherit it?

建议参考一下36贴

另:能输入汉字的话就用中文回贴吧,为了让大家更容易看懂,而且毕竟这里是中文论坛;yongjian可能只是无法输入汉字。
还有,escape应该翻译成“转义”而不是“逃逸”吧
回复 支持 反对

使用道具 举报

发表于 2006-3-18 20:34:05 | 显示全部楼层
好帖,我打算在找到工作后学习SHELL
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-18 22:02:14 | 显示全部楼层
Post by wplxb
建议参考一下36贴

另:能输入汉字的话就用中文回贴吧,为了让大家更容易看懂,而且毕竟这里是中文论坛;yongjian可能只是无法输入汉字。
还有,escape应该翻译成“转义”而不是“逃逸”吧


呵呵,多谢wplxb的建议

有些地方的翻译为逃逸,其实意思是一样的,也许我引用的不正确吧
36贴我看过了,难道不是环境变量的变量shell也能继承吗?我应该不行吧。
回复 支持 反对

使用道具 举报

发表于 2006-3-19 00:54:50 | 显示全部楼层
Post by johnny_jiang
呵呵,多谢wplxb的建议

有些地方的翻译为逃逸,其实意思是一样的,也许我引用的不正确吧
36贴我看过了,难道不是环境变量的变量shell也能继承吗?我应该不行吧。

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.
回复 支持 反对

使用道具 举报

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

本版积分规则

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