bash学习:单引号和双引号

来源:http://blog.csdn.net/jiankun_wang/article/details/4341588

谢谢原作者:)

1、单引号

Single quotes(‘’) are used to preserve the literal value of each character enclosed within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

也就是说,单引号内的所有字符都保持它本身字符的意思,而不会被bash进行解释,例如,$就是$本身而不再是bash的变量引用符;/就是/本身而不再是bash的转义字符。看下面的输出:

wangjk@wangjiankun:~$ cat single_quote.sh 
     1  #!/bin/bash 
     2 
     3  DATE='`date`' 
     4  echo "$DATE" 
     5  echo '$DATE' 
     6  echo '/$DATE' 
     7 
     8  echo 
     9  DATE="`date`" 
    10  echo "$DATE" 
    11  echo '$DATE' 
    12  echo '/$DATE' 
wangjk@wangjiankun:~$ ./single_quote.sh                                       
`date` 
$DATE 
/$DATE

Sun Jul 12 06:16:19 EDT 2009 
$DATE 
/$DATE 
wangjk@wangjiankun:~$

2、双引号

Using double quotes the literal value of all characters enclosed is preserved, except for the dollar sign, the backticks(backward single quotes, ``) and the backslash.

The dollar sign and the backticks retain their special meaning within the double quotes.

The backslash retains its meaning only when followed by dollar, backtick, double quote, backslash or newline. Within double quotes, the backslashes are removed from the input stream when followed by one of these characters. Backslashes preceding characters that don’t have a special meaning are left unmodified for processing by the shell interpreter.

也就是说,除了$、``(不是单引号)和/外,双引号内的所有字符将保持字符本身的含义而不被bash解释。$和``在双引号内,如果不被转义字符(/)转义,将无条件的保持bash下的特殊含义,而转义字符(/)是有条件的。转义字符只有后跟$、``、双引号、/和换行符五种特殊字符时才具有bash下的特殊含义:转义,换句话说,双引号中只有以上五种字符可以被转义。如果在双引号中出现后跟非以上五种字符的/字符,/就是/本身,没有什么特殊含义。

posted @ 2013-01-29 00:59  __木头鱼__  阅读(229)  评论(0编辑  收藏  举报