为了账号安全,请及时绑定邮箱和手机立即绑定

Bash中单引号和双引号之间的区别

Bash中单引号和双引号之间的区别

蛊毒传说 2019-05-22 13:09:16
Bash中单引号和双引号之间的区别在Bash中,单引号('')和双引号("")之间有什么区别?
查看完整描述

3 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

如果您指的是当您回显某些内容时会发生什么,单引号将直接回显它们之间的内容,而双引号将评估它们之间的变量并输出变量的值。

例如,这个

#!/bin/shMYVAR=sometext
echo "double quotes gives you $MYVAR"echo 'single quotes gives you $MYVAR'

会给这个:

double quotes gives you sometext
single quotes gives you $MYVAR


查看完整回答
反对 回复 2019-05-22
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

接受的答案是伟大的。我正在制作一个有助于快速理解主题的表格。解释涉及一个简单的变量a以及一个索引数组arr

如果我们设定

a=apple      # a simple variablearr=(apple)  # an indexed array with a single element

然后echo在第二列中的表达式,我们将得到第三列中显示的结果/行为。第四列解释了这种行为。

 # | Expression  | Result      | Comments
---+-------------+-------------+--------------------------------------------------------------------
 1 | "$a"        | apple       | variables are expanded inside ""
 2 | '$a'        | $a          | variables are not expanded inside ''
 3 | "'$a'"      | 'apple'     | '' has no special meaning inside ""
 4 | '"$a"'      | "$a"        | "" is treated literally inside ''
 5 | '\''        | **invalid** | can not escape a ' within ''; use "'" or $'\'' (ANSI-C quoting)
 6 | "red$arocks"| red         | $arocks does not expand $a; use ${a}rocks to preserve $a
 7 | "redapple$" | redapple$   | $ followed by no variable name evaluates to $
 8 | '\"'        | \"          | \ has no special meaning inside ''
 9 | "\'"        | \'          | \' is interpreted inside "" but has no significance for '
10 | "\""        | "           | \" is interpreted inside ""
11 | "*"         | *           | glob does not work inside "" or ''
12 | "\t\n"      | \t\n        | \t and \n have no special meaning inside "" or ''; use ANSI-C quoting
13 | "`echo hi`" | hi          | `` and $() are evaluated inside ""
14 | '`echo hi`' | `echo hi`   | `` and $() are not evaluated inside ''
15 | '${arr[0]}' | ${arr[0]}   | array access not possible inside ''
16 | "${arr[0]}" | apple       | array access works inside ""
17 | $'$a\''     | $a'         | single quotes can be escaped inside ANSI-C quoting
18 | "$'\t'"     | $'\t'       | ANSI-C quoting is not interpreted inside ""
19 | '!cmd'      | !cmd        | history expansion character '!' is ignored inside ''
20 | "!cmd"      | cmd args    | expands to the most recent command matching "cmd"
21 | $'!cmd'     | !cmd        | history expansion character '!' is ignored inside ANSI-C quotes
---+-------------+-------------+--------------------------------------------------------------------

也可以看看:


查看完整回答
反对 回复 2019-05-22
  • 3 回答
  • 0 关注
  • 1101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信