分享

shell编程中的引用

 cwhbox 2011-08-18

shell编程中的引用

2011/01/24 by edsionteLeave a reply ?

shell编程语言中有四种引用符号,分别是单引号(”),双引号(“”),反单引号(“)和反斜杠。前三种符号必须成对出现来使用,而后者则单独使用。有时候我们需要shell将某些字符进行特殊解释,此时引用符号就派上了用场。

单引号

1.整体赋值

在对变量进行赋值时,使用单引号将中间有空格的字符引用后赋值给变量后,shell会将这些字符看作成一个整体来处理。比如,我们需要在name文件中查找edsionte wu这个姓名:

01edsionte@edsionte-desktop:~/shelltest$ cat name
02edsionte
03zhanyu
04miao
05edsionte wu
06edsionteli
07edsionte@edsionte-desktop:~/shelltest$ grep edsionte wu name
08grep: wu: 没有那个文件或目录
09name:edsionte
10name:edsionte wu
11name:edsionteli

此时grep命令产生了错误信息。由于edsionte和wu字符之间有空格,grep将edsionte视为要查找的字符,而wu和name都是所要查找的文件。如果我们将edsionte和wu放入单引号内,就可以正确得到结果:

1edsionte@edsionte-desktop:~/shelltest$ grep 'edsionte wu' name
2edsionte wu

通过使用单引号,shell从上面的那条命令中提取到edsionte wu和name两个参数,再将这两个参数传递给grep命令。

2.保留字符

使用单引号将字符引用后,shell会原封不动的保留单引号内的字符,比如:

1edsionte@edsionte-desktop:~/shelltest$ echo edsionte              wu
2edsionte wu
3edsionte@edsionte-desktop:~/shelltest$ echo 'edsionte              wu'
4edsionte              wu

不使用单引号时,shell会将字符之间的多个空格缩减至一个;如果使用了单引号,那么shell会原封不动将单引号中的内容提取出来,送至echo命令。 同样,由于单引号原封不动的特性,处于单引号内部的特殊字符不会被shell解释。比如:

1edsionte@edsionte-desktop:~/shelltest$ ls
2gender  name  number
3edsionte@edsionte-desktop:~/shelltest$ echo *
4gender name number
5edsionte@edsionte-desktop:~/shelltest$ echo '*'
6*

可以看到,特殊字符*并不会被单引号所解释。接下来的这个例子会让你更深入的理解上述两个功能。比如:

1edsionte@edsionte-desktop:~/shelltest$ files='I have these files: *'
2edsionte@edsionte-desktop:~/shelltest$ echo $files
3I 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命令。

双引号

双引号和单引号的功能几乎相同,只不过双引号对个别几个特殊字符会做解释,而不像单引号那样完全忽略所有特殊字符。这几个特殊字符为:美元符号($),反引号(“),反斜杠(/)。比如:

1edsionte@edsionte-laptop:~/shelltest$ ls
2info  name  nf  status  test  whos
3edsionte@edsionte-laptop:~/shelltest$ x=*
4edsionte@edsionte-laptop:~/shelltest$ echo $x
5info name nf status test whos
6edsionte@edsionte-laptop:~/shelltest$ echo '$x'
7$x
8edsionte@edsionte-laptop:~/shelltest$ echo "$x"
9*

可以看到,shell对位于双引号内的$进行了解释,也就是将其解释为对x变量的引用。除此之外,双引号和单引号并无太大差异。比如:

1edsionte@edsionte-laptop:~/shelltest$ x="edsionte    wu"
2edsionte@edsionte-laptop:~/shelltest$ echo $x
3edsionte wu
4edsionte@edsionte-laptop:~/shelltest$ echo "$x"
5edsionte    wu
6edsionte@edsionte-laptop:~/shelltest$ echo '$x'
7$x

对于第一条的赋值语句,不管使用单引号还是双引号,效果是一样的,都是将edsionte wu作为一个整体赋值给变量x。

反引号

在命令A中使用反引号的目的并不是希望shell保留某些特殊字符的本意,而是将其他的命令B包含起来,并把命令B的输出插入到命令B所在的位置。其格式为:
cmdA `cmdB`

1比如通过反引号的引用在echo命令中使用date命令:
2edsionte@edsionte-desktop:~/shelltest$ echo 'Now is `date`'
3Now is `date`
4edsionte@edsionte-desktop:~/shelltest$ echo "Now is `date`"
5Now is 2011年 01月 25日 星期二 16:48:40 CST

由于单引号对任何字符都不作特殊解释,因此反引号在单引号中并不起作用。但是在双引号中,反引号中的date命令被shell解释了。
由于双引号

反斜杠

1.消除特殊含义

如果在一些特殊字符前面加上反斜杠,则会消除这些字符的特殊含义。因此\c的功效相当于’c'。比如:

1edsionte@edsionte-desktop:~/shelltest$ x=*
2edsionte@edsionte-desktop:~/shelltest$ echo $x
3gender name number
4edsionte@edsionte-desktop:~/shelltest$ echo \$x
5$x

可以看到,在$前面加入反斜杠可以去除$的引用含义而将$本身显示出来。

2.续行

当反斜杠位于一行的最后一个字符时,它会起到续行的作用。比如:

1edsionte@edsionte-desktop:~/shelltest$ file=edsion\
2> te
3edsionte@edsionte-desktop:~/shelltest$ echo $file
4edsionte

使用反斜杠进行续行后,反斜杠前后的字符之间并没有分隔符。

3.在双引号中使用 反斜杠

如果我们希望shell保留双引号中的特殊字符(即$、“和\)的含义,那么反斜杠就派上了用场。比如:

1edsionte@edsionte-desktop:~/shelltest$ x=5
2edsionte@edsionte-desktop:~/shelltest$ echo "The value of \$x is $x"
3The value of $x is 5

end

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多