一、单引号和双引号

首先, 单引号和双引号,都是为了解决中间有空格的问题。

因为空格在linux中时作为一个很典型的分隔符,比如 string1=this is a string,这样执行就会报错。为了避免这个问题,因此就产生了单引号和双引号。他们的区别在于,单引号将剥夺其中的所有字符的特殊含义,而双引号中的'$'(参数替换)和'`'(命令替换)是例外。所以,两者基本上没有什么区别,除非在内容中遇到了参数替换符$和命令替换符`。

所以下面的结果:
num=3
echo ‘$num’
$num
echo “$num”
3
所以,如果需要在双引号””里面使用这两种符号,需要用反斜杠转义。


二、反引号``

在执行一条命令时,会先将其中的 `` 语句当作命令执行一遍,再将结果加入到原命令中重新执行,例如:
echo `ls`
会先执行 ls 得到 xx.sh等,再替换原命令为:
echo xx.sh
最后执行结果为
xx.sh

[@entmcnode15] try $ ./quote.sh 
in double quote,this is my friend
in single quote,this is $num
the date is Wed Sep 10 16:13:56 CEST 2014
./quote.sh: line 6: friend: command not found

[@entmcnode15] try $ cat quote.sh
#!/bin/bash
num="my friend"
echo "in double quote,this is $num"
echo 'in single quote,this is $num'
echo the date is `date`
num1=my friend
echo $num1
解释:

从例中可知,当字符串有空格时,num="my friend" 将双引号中的字符串赋给num,但是num1=my friend直接是语法错误;''中直接将$num当作字符串。




Logo

更多推荐