Quoting is used to remove the special meaning of certain character or words to the shell
引号用于防止一些特殊的字符被 Shell 扩展
'
(单引号)用于保留字符的字面意思,包括各种特殊字符($
,*
,\
)
例如
[0x00@0xff1 tmp]$ a=1
[0x00@0xff1 tmp]$ echo "${a}"
1
[0x00@0xff1 tmp]$ echo '${a}'
${a}
需要注意的一点是单引号内不能出现单引号,即使单引号使用了 \
来转义
[0x00@0xff1 tmp]$ echo '\''
> ^C
“
(双引号)保留大多数字符的字面意思,除了 $
, `
, \
会被 Bash 扩展
[0x00@0xff1 tmp]$ a=1
[0x00@0xff1 tmp]$ echo "${a}"
1
[0x00@0xff1 tmp]$ echo "`whoami`"
0x00
A non-quoted backslash
\
is the Bash escape character. It preserves the literal value of the next character that follows
和其他大部分编程语言一样 \
(backslash) 都表示转义
[0x00@0xff1 tmp]$ a=1
[0x00@0xff1 tmp]$ echo "\${a}"
${a}
[0x00@0xff1 tmp]$ echo "${a}"
1