shell编程语言中有四种引用符号,分别是单引号(”),双引号(“”),反单引号(“)和反斜杠。前三种符号必须成对出现来使用,而后者则单独使用。有时候我们需要shell将某些字符进行特殊解释,此时引用符号就派上了用场。
单引号
1.整体赋值
在对变量进行赋值时,使用单引号将中间有空格的字符引用后赋值给变量后,shell会将这些字符看作成一个整体来处理。比如,我们需要在name文件中查找edsionte wu这个姓名:
edsionte@edsionte-desktop:~/shelltest$ cat name edsionte zhanyu miao edsionte wu edsionteli edsionte@edsionte-desktop:~/shelltest$ grep edsionte wu name grep: wu: 没有那个文件或目录 name:edsionte name:edsionte wu name:edsionteli
此时grep命令产生了错误信息。由于edsionte和wu字符之间有空格,grep将edsionte视为要查找的字符,而wu和name都是所要查找的文件。如果我们将edsionte和wu放入单引号内,就可以正确得到结果:
edsionte@edsionte-desktop:~/shelltest$ grep 'edsionte wu' name edsionte wu
通过使用单引号,shell从上面的那条命令中提取到edsionte wu和name两个参数,再将这两个参数传递给grep命令。
2.保留字符
使用单引号将字符引用后,shell会原封不动的保留单引号内的字符,比如:
edsionte@edsionte-desktop:~/shelltest$ echo edsionte wu edsionte wu edsionte@edsionte-desktop:~/shelltest$ echo 'edsionte wu' edsionte wu
不使用单引号时,shell会将字符之间的多个空格缩减至一个;如果使用了单引号,那么shell会原封不动将单引号中的内容提取出来,送至echo命令。 同样,由于单引号原封不动的特性,处于单引号内部的特殊字符不会被shell解释。比如:
edsionte@edsionte-desktop:~/shelltest$ ls gender name number edsionte@edsionte-desktop:~/shelltest$ echo * gender name number edsionte@edsionte-desktop:~/shelltest$ echo '*' *
可以看到,特殊字符*并不会被单引号所解释。接下来的这个例子会让你更深入的理解上述两个功能。比如:
edsionte@edsionte-desktop:~/shelltest$ files='I have these files: *' edsionte@edsionte-desktop:~/shelltest$ echo $files I have these files: gender name number
可能你会有疑问:处于单引号内的*为什么会被shell解释呢?如果你理解了这里的单引号所起的作用就回消除上面的疑惑。对于echo $files命令来说,shell会先将$file替换为I have these files: *,也就是说这里的单引号是为了将上述字符整体赋值给files变量;然后shell会将*替换为当前目录下的所有文件名;最后将I have these files: gender name number作为一个整体传给echo命令。
双引号
双引号和单引号的功能几乎相同,只不过双引号对个别几个特殊字符会做解释,而不像单引号那样完全忽略所有特殊字符。这几个特殊字符为:美元符号($),反引号(“),反斜杠(/)。比如:
edsionte@edsionte-laptop:~/shelltest$ ls info name nf status test whos edsionte@edsionte-laptop:~/shelltest$ x=* edsionte@edsionte-laptop:~/shelltest$ echo $x info name nf status test whos edsionte@edsionte-laptop:~/shelltest$ echo '$x' $x edsionte@edsionte-laptop:~/shelltest$ echo "$x" *
可以看到,shell对位于双引号内的$进行了解释,也就是将其解释为对x变量的引用。除此之外,双引号和单引号并无太大差异。比如:
edsionte@edsionte-laptop:~/shelltest$ x="edsionte wu" edsionte@edsionte-laptop:~/shelltest$ echo $x edsionte wu edsionte@edsionte-laptop:~/shelltest$ echo "$x" edsionte wu edsionte@edsionte-laptop:~/shelltest$ echo '$x' $x
对于第一条的赋值语句,不管使用单引号还是双引号,效果是一样的,都是将edsionte wu作为一个整体赋值给变量x。
反引号
在命令A中使用反引号的目的并不是希望shell保留某些特殊字符的本意,而是将其他的命令B包含起来,并把命令B的输出插入到命令B所在的位置。其格式为:
cmdA `cmdB`
比如通过反引号的引用在echo命令中使用date命令: edsionte@edsionte-desktop:~/shelltest$ echo 'Now is `date`' Now is `date` edsionte@edsionte-desktop:~/shelltest$ echo "Now is `date`" Now is 2011年 01月 25日 星期二 16:48:40 CST
由于单引号对任何字符都不作特殊解释,因此反引号在单引号中并不起作用。但是在双引号中,反引号中的date命令被shell解释了。
由于双引号
反斜杠
1.消除特殊含义
如果在一些特殊字符前面加上反斜杠,则会消除这些字符的特殊含义。因此\c的功效相当于’c’。比如:
edsionte@edsionte-desktop:~/shelltest$ x=* edsionte@edsionte-desktop:~/shelltest$ echo $x gender name number edsionte@edsionte-desktop:~/shelltest$ echo \$x $x
可以看到,在$前面加入反斜杠可以去除$的引用含义而将$本身显示出来。
2.续行
当反斜杠位于一行的最后一个字符时,它会起到续行的作用。比如:
edsionte@edsionte-desktop:~/shelltest$ file=edsion\ > te edsionte@edsionte-desktop:~/shelltest$ echo $file edsionte
使用反斜杠进行续行后,反斜杠前后的字符之间并没有分隔符。
3.在双引号中使用 反斜杠
如果我们希望shell保留双引号中的特殊字符(即$、“和\)的含义,那么反斜杠就派上了用场。比如:
edsionte@edsionte-desktop:~/shelltest$ x=5 edsionte@edsionte-desktop:~/shelltest$ echo "The value of \$x is $x" The value of $x is 5
end