Single Quote
Use single quote when you want to literally print everything inside the single quote. Even the special variables such as $HOSTNAME will be print as $HOSTNAME instead of printing the name of the Linux host.
$ echo 'Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD' Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD
Double Quote
Use double quotes when you want to display the real meaning of special variables.
$ echo "Hostname=$HOSTNAME ; Current User=`whoami` ; Message=\$ is USD" Hostname=dev-db ; Current User=ramesh ; Message=$ is USD
Double quotes will remove the special meaning of all characters except the following:
$Parameter Substitution.`Backquotes\$Literal Dollar Sign.\´Literal Backquote.\”Embedded Doublequote.\\Embedded Backslashes.
Reference
本文详细解析了在Shell脚本中单引号和双引号的区别及使用场景。单引号会将内部内容作为字面意义打印,而双引号则会解析变量和命令替换。此外,还介绍了双引号内保留特殊含义的字符。
2245

被折叠的 条评论
为什么被折叠?



