LinuxSir.cn,穿越时空的Linuxsir!

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

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

[复制链接]
发表于 2006-3-15 10:04:36 | 显示全部楼层 |阅读模式

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


How does that happen? I found there's a passage in bash manual describing command substitution.

When the old-style backquote form of substitution  is  used,  backslash retains  its  literal  meaning except when followed by $, `, or \.  The first backquote not preceded by a backslash terminates the command substitution.   When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.


So it means when backslash followed by $, it is supposed to escape next character, but it seems different from my test.

I'm fuzzy on the usage of backslash in backquote.
发表于 2006-3-15 10:09:12 | 显示全部楼层
echo \$x
$x
echo $x
a

一个进程替换。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-15 11:32:14 | 显示全部楼层
Post by seamonkey
echo \$x
$x
echo $x
a

一个进程替换。


process substitution?

我不是很理解,seamonkey兄可否在详细解释一下.
回复 支持 反对

使用道具 举报

发表于 2006-3-15 12:44:37 | 显示全部楼层
\$表示这里是$字符,而不是$后跟变量
$是特殊字符,要用反斜缸(backslash)才能表示出
如果$不是特殊字符echo $x显示为$x,但echo $x理解成显示x变量。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-15 12:56:16 | 显示全部楼层
Post by chinajz
\$表示这里是$字符,而不是$后跟变量
$是特殊字符,要用反斜缸(backslash)才能表示出
如果$不是特殊字符echo $x显示为$x,但echo $x理解成显示x变量。


Maybe I didn't voice it clearly. I know this is a variable substitution, but my question is when they're embedded in old-version command substitution.


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


I think there're several steps happened from reference of bash manual:
1. command substitution: `echo \$x` -> $x
2. word splitting -> $x
3. no pattern match
4. execution

So I think it should output $x instead of a. I hope the master-hand can help me point out where I figure it out incorrectly.
回复 支持 反对

使用道具 举报

发表于 2006-3-15 13:06:57 | 显示全部楼层
bash# x=a
bash# echo ’echo \$x’
bash# $x
bash# echo `echo \$x`
bash# a
这里$是特殊字符要不然可这样变化:
echo `uname -r`
echo $(uname -r)
这样就明白了
回复 支持 反对

使用道具 举报

发表于 2006-3-15 13:11:00 | 显示全部楼层
我又讲错了,Command substitution 命令替换。
回复 支持 反对

使用道具 举报

发表于 2006-3-15 13:12:45 | 显示全部楼层
`x y`=$(x y)
``说明是变量,而不是字符
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-15 13:30:55 | 显示全部楼层
Post by chinajz
`x y`=$(x y)
``说明是变量,而不是字符


不是很理解你的解释
bash# x=a
bash# echo ’echo \$x’
bash# $x
bash# echo `echo \$x`
bash# a


蓝色的应该是命令替换,那么上面红色的命令是什么呢?
``和$()是有区别的,在bash的手册页上面,请看我一楼的引用
小弟的问题是当命令替换的结果为什么是a,我想了解一下bash到底是怎么解释这个命令的.
回复 支持 反对

使用道具 举报

发表于 2006-3-15 13:46:51 | 显示全部楼层
debian:~# x=a
debian:~# b=`echo \$x`
debian:~# echo $b
a
我还以为你把` `看成' '
``和$()表示方法当然不同,但相同的地方可能更多,不争论了,哈哈,大家学习提高
回复 支持 反对

使用道具 举报

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

本版积分规则

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