
单引号为强引用,即将单引号中的内容原样输出,而不会对任何变量函数进行解析。比如在 Shell 中输入以下代码:
_str_="hello world"
echo '$_str_'结果输出:
$_str_在双引号内,会解析的符号:` $ \
除此之外,其余符号都原样输出。比如在 Shell 中输入以下代码:
_str_="hello world"
echo "$_str_"结果输出:
hello world反引号和 () 的功能一样,都是进行命令替换,将反引号或 () 中的字符串做为命令来执行。比如在 Shell 中输入以下代码:
_str_="hello world"
echo `$_str_`结果输出:
Command 'hello' not found, but can be installed with:
sudo apt install hello
sudo apt install hello-traditional这是因为反引号将变量 $_str_ 当作命令来处理了。
同双引号,只是在对变量进行赋值、引用时,如果含有特殊字符(比如空格),则需要在特殊字符前添加转义字符 \ 。
比如在Shell中输入以下代码:
_str_=hello world
echo $_str_结果输出:
Command 'world' not found, did you mean:
command 'tworld' from deb tworld
Try: sudo apt install <deb name>因为含有特殊字符空格,故只将 hello 赋值给了 _str_,而将 world 当成了命令进行处理。如果在 Shell 中输入以下代码:
_str_=hello\ world
echo $_str_结果输出:
hello world因加上了转义字符,故为正确赋值。