bash学习之七:单引号和双引号

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下的特殊含义:转义,换句话说,双引号中只有以上五种字符可以被转义。如果在双引号中出现后跟非以上五种字符的/字符,/就是/本身,没有什么特殊含义。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值