Difference between single and double quotes in Bash
在bash中,单引号(
单引号不会插入任何内容,但双引号会插入。例如:变量、倒计时、某些
例子:
1 2 3 4 | $ echo"$(echo"upg")" upg $ echo '$(echo"upg")' $(echo"upg") |
bash手册中有这样的内容:
3.1.2.2 Single Quotes
Enclosing characters in single quotes (
' ) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.3.1.2.3 Double Quotes
Enclosing characters in double quotes (
" ) preserves the literal value of all characters within the quotes, with the exception of$ ,` ,\ , and, when history expansion is enabled,! . The characters$ and` retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters:$ ,` ," ,\ , or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an! appearing in double quotes is escaped using a backslash. The backslash preceding the! is not removed.The special parameters
* and@ have special meaning when in double quotes (see Shell Parameter Expansion).
如果你指的是当你回送某个东西时会发生什么,那么单引号将直接回送你在它们之间拥有的东西,而双引号将评估它们之间的变量并输出变量的值。
例如,这个
1 2 3 4 | #!/bin/sh MYVAR=sometext echo"double quotes gives you $MYVAR" echo 'single quotes gives you $MYVAR' |
将给予:
1 2 | double quotes gives you sometext single quotes gives you $MYVAR |
公认的答案很好。我正在做一张有助于快速理解主题的桌子。解释包括一个简单的变量
如果我们设置
1 2 | a=apple # a simple variable arr=(apple) # an indexed array with a single element |
然后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # | 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 " | \t | \t and 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 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" ---+-------------+-------------+-------------------------------------------------------------------- |
参见:
- ANSI-C引用
$'' -gnu bash手册 - 使用
$"" 的本地翻译-gnu bash手册 - 引号的三点公式
其他人解释得很好,只想举几个简单的例子。
可以在文本周围使用单引号,以防止外壳解释任何特殊字符。当用单引号括起来时,美元符号、空格、符号、星号和其他特殊字符都被忽略。
1 | $ echo 'All sorts of things are ignored in single quotes, like $ & * ; |.' |
它将给出:
1 | All sorts of things are ignored in single quotes, like $ & * ; |. |
唯一不能放在单引号内的是单引号。
双引号的作用类似于单引号,但双引号仍然允许shell解释美元符号、反引号和反斜杠。众所周知,反斜杠会阻止解释单个特殊字符。如果需要将美元符号用作文本而不是变量,这在双引号中非常有用。它还允许对双引号进行转义,这样它们就不会被解释为带引号字符串的结尾。
1 | $ echo"Here's how we can use single ' and double " quotes within double quotes" |
它将给出:
1 | Here's how we can use single ' and double" quotes within double quotes |
也可以注意到,撇号(否则会被解释为带引号字符串的开头)在双引号中被忽略。然而,变量在双引号内被解释并替换为其值。
1 | $ echo"The current Oracle SID is $ORACLE_SID" |
它将给出:
1 | The current Oracle SID is test |
后引号完全不同于单引号或双引号。反引号不是用来阻止特殊字符的解释,而是实际强制执行它们所包含的命令。在执行封闭的命令之后,它们的输出将被替换为原始行中的反引号。这将通过一个例子更清楚。
1 2 | $ today=`date '+%A, %B %d, %Y'` $ echo $today |
它将给出:
1 | Monday, September 28, 2015 |
(P)既然这是事实上的答案,当我处理在EDOCX1中的字母0,我将在一个更多的问题上错过在答案above,当他们处理与壳牌中的Arithmetic操作员。(p)(P)The EDOCX1 original 0 Shell supports two ways do arithmetic operation,one defined by the built-in EDOCX1 English 2 command and the EDOCX1《前评估者的Arithmetic expression while the latter is more of a compound statement》。(p)(P)重要的是要了解到,阿里特梅蒂奇的表达方式是用以东X1进行的。因此,我们希望能够得到捐助。(p)(P)See this example when using EDOCX1 theocx1(p)字母名称(P)如果在这里唯一的东西是绝对的,那么这里不需要可变扩张,考虑一个案例(p)字母名称(P)Would fail miserably,as the EDOCX1 plography 6 under single quotes would not expand and needs to be double-quoted as(p)字母名称(P)This should be one of the reasons,the EDOCX1 universal 3 welcx1 should always be considered over using EDOCX1 universal 2.由于认识到了这一点,所以没有任何内容可以讨论。The previous example using EDOCX1 English 2 Can be simply written as(p)字母名称Always remember to use EDOCX1 universal 3 without single quotes(P)Though the EDOCX1 substantive 3 can be used with double-quotes,there is no purpose to it as the result of it cannot contain a content that would uld need the double-quote.只要确保这不是一件事。(p)字母名称(P)可能是在使用以东X1的一些特殊案例中,3是指一个独奏的独奏者的独奏中,你需要在一种方式中插入这样的内容,即操作者是没有任何优势的,或者是在两种情况下。e.g.审议一个案件,当你建议使用操作人员内的一个EDOCX1,一个或多个字母13(p)字母名称(P)Notice the use of nested double-quotes inside,without which the string EDOCX1 original 14 is passed to EDOCX1(p)
当
使用
通过翻译/转换,我的意思是:单引号内的任何内容都不会"翻译"为其值。它们将被视为在引号内。例如: