Shell 单引号 双引号 反引号

来源:互联网 发布:创业网络平台建设 编辑:程序博客网 时间:2024/04/30 18:50


在shell中引号分为三种:单引号,双引号和反引号。


单引号 ‘
由单引号括起来的字符都作为普通字符出现。特殊字符用单引号括起来以后,也会失去原有意义,而只作为普通字符解释。例如:

$ string=’$PATH’

$ echo $string

$PATH

$

可见$保持了其本身的含义,作为普通字符出现。


双引号

双引号内的$符号不会被作为单个美元符号,也就是说如果字符串中包含了类似$A $B这样的字符串时,实际上shell还是会执行取$A $B值的动作。看实际例子:
[root@node82 ~]# A=AC
[root@node82 ~]# word="The champion is $A" 这里的变量A的值会生效
[root@node82 ~]# echo $word
The champion is AC


反引号 `
反引号(`)这个字符所对应的键一般位于键盘的左上角,不要将其同单引号(’)混淆。反引号括起来的字符串被shell解释为命令行,在执行时,shell首先执行该命令行,并以它的标准输出结果取代整个反引号(包括两个反引号)部分。例如:

$ string=”current directory is `pwd`”

$ echo $string

current directour is /home/xyz


使用反引号引用命令
反引号可以把命令赋值给一个变量,那么可以把这个变量作为linux命令的另外的一个名称,这样的话在命令行直接输入$变量名就可以执行这个linux命令。


转义字符   \

\X   escapesthe characterX. This has the effect of "quoting"X, equivalent to'X'. The \ may be used to quote " and ', so they are expressed literally.


:
null command [colon](冒号).This is the shell equivalent(等价物) of a "NOP" (no op, a do-nothing operation).

 It may be considered a synonym(同义词) for the shell builtin(内建) true. The ":" command is itself a Bashbuiltin, and
its exit status is true(0).


:=

{str:=expr}

如果变量str不为空,${str:=expr}就等于str的值,若str为空,就把expr的值赋值给str。