In Bash, what are the differences between single quotes (''
) and double quotes (""
)?
在Bash中,单引号('')和双引号(“”)之间有什么区别?
6 个解决方案
#1
Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain \
escapes, etc.
单引号不会插入任何内容,但双引号会。例如:变量,反引号,某些\转义等。
Example:
$ echo "$(echo "upg")"upg$ echo '$(echo "upg")'$(echo "upg")
The Bash manual has this to say:
Bash手册有这样的说法:
3.1.2.2单引号
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双引号
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.用双引号括起来的字符(“)保留引号内所有字符的字面值,但$,`,\除外,并且,当启用历史扩展时,!。字符$和`在double中保留它们的特殊含义引号(参见Shell Expansions)。反斜杠只有在跟随以下字符之一时才保留其特殊含义:$,`,“,\或newline。在双引号内,将删除后跟其中一个字符的反斜杠。没有特殊含义的字符前面的反斜杠不做修改。双引号可以在双引号内引用,前面加一个反斜杠。如果启用,将执行历史记录扩展,除非!出现在双引号中使用反斜杠进行转义。之前的反斜杠!没有删除。
The special parameters
*
and@
have special meaning when in double quotes (see Shell Parameter Expansion).在双引号中,特殊参数*和@具有特殊含义(参见Shell参数扩展)。
#2
If you're referring to what happens when you echo something, the single quotes will literally echo what you have between them, while the double quotes will evaluate variables between them and output the value of the variable.
如果您指的是当您回显某些内容时会发生什么,单引号将直接回显它们之间的内容,而双引号将评估它们之间的变量并输出变量的值。
For example, this
例如,这个
#!/bin/shMYVAR=sometextecho "double quotes gives you $MYVAR"echo 'single quotes gives you $MYVAR'
will give this:
会给这个:
double quotes gives you sometextsingle quotes gives you $MYVAR
#3
The accepted answer is great. I am making a table that helps in quick comprehension of the topic. The explanation involves a simple variable a
as well as an indexed array arr
.
接受的答案很棒。我正在制作一个有助于快速理解主题的表格。解释涉及一个简单的变量a和一个索引数组arr。
If we set
如果我们设定
a=apple # a simple variablearr=(apple) # an indexed array with a single element
and then echo
the expression in the second column, we would get the result / behavior shown in the third column. The fourth column explains the behavior.
然后在第二列中回显表达式,我们将得到第三列中显示的结果/行为。第四列解释了这种行为。
# | 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 quoting13 | "`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 quoting18 | "$'\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---+-------------+-------------+--------------------------------------------------------------------
See also:
- ANSI-C quoting with
$''
- GNU Bash Manual - Locale translation with
$""
- GNU Bash Manual - A three-point formula for quotes
ANSI-C引用$'' - GNU Bash手册
带有$“”的语言环境翻译 - GNU Bash手册
报价的三点公式
#4
Others explained very well and just want to give with simple examples.
其他人解释得很好,只想用简单的例子给出。
Single quotes can be used around text to prevent the shell from interpreting any special characters. Dollar signs, spaces, ampersands, asterisks and other special characters are all ignored when enclosed within single quotes.
可以在文本周围使用单引号来防止shell解释任何特殊字符。当用单引号括起来时,美元符号,空格,&符号,星号和其他特殊字符都会被忽略。
$ echo 'All sorts of things are ignored in single quotes, like $ & * ; |.'
It will give this:
它会给出这样的:
All sorts of things are ignored in single quotes, like $ & * ; |.
The only thing that cannot be put within single quotes is a single quote.
唯一不能放在单引号内的是单引号。
Double quotes act similarly to single quotes, except double quotes still allow the shell to interpret dollar signs, back quotes and backslashes. It is already known that backslashes prevent a single special character from being interpreted. This can be useful within double quotes if a dollar sign needs to be used as text instead of for a variable. It also allows double quotes to be escaped so they are not interpreted as the end of a quoted string.
双引号的行为类似于单引号,除了双引号仍允许shell解释美元符号,后引号和反斜杠。众所周知,反斜杠可以防止解释单个特殊字符。如果需要将美元符号用作文本而不是变量,则在双引号内这可能很有用。它还允许对双引号进行转义,因此它们不会被解释为带引号的字符串的结尾。
$ echo "Here's how we can use single ' and double \" quotes within double quotes"
It will give this:
它会给出这样的:
Here's how we can use single ' and double " quotes within double quotes
It may also be noticed that the apostrophe, which would otherwise be interpreted as the beginning of a quoted string, is ignored within double quotes. Variables, however, are interpreted and substituted with their values within double quotes.
还可以注意到,撇号(否则将被解释为带引号的字符串的开头)在双引号内被忽略。但是,变量被解释并用双引号内的值代替。
$ echo "The current Oracle SID is $ORACLE_SID"
It will give this:
它会给出这样的:
The current Oracle SID is test
Back quotes are wholly unlike single or double quotes. Instead of being used to prevent the interpretation of special characters, back quotes actually force the execution of the commands they enclose. After the enclosed commands are executed, their output is substituted in place of the back quotes in the original line. This will be clearer with an example.
后引号完全不同于单引号或双引号。后引号实际上强制执行它们所包含的命令,而不是用于防止特殊字符的解释。执行所附的命令后,将替换它们的输出以代替原始行中的后引号。通过一个例子,这将更加清晰。
$ today=`date '+%A, %B %d, %Y'`$ echo $today
It will give this:
它会给出这样的:
Monday, September 28, 2015
#5
Since this is the de facto answer when dealing with quotes in bash
, I'll add upon one more point missed in the answers above, when dealing with the arithmetic operators in the shell.
因为在处理bash中的引号时这是事实上的答案,所以在处理shell中的算术运算符时,我将在上面的答案中添加一个错过的点。
The bash
shell supports two ways do arithmetic operation, one defined by the built-in let
command and the $((..))
operator. The former evaluates an arithmetic expression while the latter is more of a compound statement.
bash shell支持两种算术运算方式,一种由内置let命令和$((..))运算符定义。前者评估算术表达式,而后者更多是复合语句。
It is important to understand that the arithmetic expression used with let
undergoes word-splitting, pathname expansion just like any other shell commands. So proper quoting and escaping needs to be done.
重要的是要理解与let一起使用的算术表达式经历了分词,路径名扩展,就像任何其他shell命令一样。因此需要进行适当的引用和转义。
See this example when using let
使用let时请参阅此示例
let 'foo = 2 + 1'echo $foo3
Using single quotes here is absolutely fine here, as there is no need for variable expansions here, consider a case of
这里使用单引号绝对没问题,因为这里不需要变量扩展,考虑一下
bar=1let 'foo = $bar + 1'
would fail miserably, as the $bar
under single quotes would not expand and needs to be double-quoted as
会失败,因为单引号下的$ bar不会扩大,需要双引号
let 'foo = '"$bar"' + 1'
This should be one of the reasons, the $((..))
should always be considered over using let
. Because inside it, the contents aren't subject to word-splitting. The previous example using let
can be simply written as
这应该是其中一个原因,$((..))应该始终考虑使用let。因为在其内部,内容不受分词的影响。使用let的前一个示例可以简单地写为
(( bar=1, foo = bar + 1 ))
Always remember to use $((..))
without single quotes
Though the $((..))
can be used with double-quotes, there is no purpose to it as the result of it cannot contain a content that would need the double-quote. Just ensure it is not single quoted.
尽管$((..))可以与双引号一起使用,但它没有任何意图,因为它不能包含需要双引号的内容。确保它不是单引号。
printf '%d\n' '$((1+1))'-bash: printf: $((1+1)): invalid numberprintf '%d\n' $((1+1))2printf '%d\n' "$((1+1))"2
May be in some special cases of using the $((..))
operator inside a single quoted string, you need to interpolate quotes in a way that the operator either is left unquoted or under double quotes. E.g. consider a case, when you are tying to use the operator inside a curl
statement to pass a counter every time a request is made, do
可能在一些在单引号字符串中使用$((..))运算符的特殊情况下,您需要以操作符未加引号或双引号的方式插入引号。例如。考虑一个案例,当你想要在curl语句中使用运算符来在每次请求时传递一个计数器时,
curl http://myurl.com --data-binary '{"requestCounter":'"$((reqcnt++))"'}'
Notice the use of nested double-quotes inside, without which the literal string $((reqcnt++))
is passed to requestCounter
field.
注意在内部使用嵌套的双引号,没有它,文字字符串$((reqcnt ++))将传递给requestCounter字段。
#6
There is a clear distinction between the usage of ' '
and " "
.
“和”的用法有明显的区别。
When ' '
is used around anything, there is no "transformation or translation" done. It is printed as it is.
什么时候使用'',没有“转换或翻译”。它是按原样打印的。
With " "
, whatever it surrounds, is "translated or transformed" into its value.
随着“”,无论它包围什么,都被“翻译或转化”为其价值。
By translation/ transformation I mean the following:Anything within the single quotes will not be "translated" to their values. They will be taken as they are inside quotes. Example: a=23
, then echo '$a'
will produce $a
on standard output. Whereas echo "$a"
will produce 23
on standard output.
通过翻译/转换,我的意思是:单引号内的任何内容都不会被“翻译”到它们的值。它们将被引用,因为它们在引号内。例如:a = 23,那么echo'$ a'将在标准输出上产生$ a。回声“$ a”将在标准输出上产生23。
#1
Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain \
escapes, etc.
单引号不会插入任何内容,但双引号会。例如:变量,反引号,某些\转义等。
Example:
$ echo "$(echo "upg")"upg$ echo '$(echo "upg")'$(echo "upg")
The Bash manual has this to say:
Bash手册有这样的说法:
3.1.2.2单引号
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双引号
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.用双引号括起来的字符(“)保留引号内所有字符的字面值,但$,`,\除外,并且,当启用历史扩展时,!。字符$和`在double中保留它们的特殊含义引号(参见Shell Expansions)。反斜杠只有在跟随以下字符之一时才保留其特殊含义:$,`,“,\或newline。在双引号内,将删除后跟其中一个字符的反斜杠。没有特殊含义的字符前面的反斜杠不做修改。双引号可以在双引号内引用,前面加一个反斜杠。如果启用,将执行历史记录扩展,除非!出现在双引号中使用反斜杠进行转义。之前的反斜杠!没有删除。
The special parameters
*
and@
have special meaning when in double quotes (see Shell Parameter Expansion).在双引号中,特殊参数*和@具有特殊含义(参见Shell参数扩展)。
#2
If you're referring to what happens when you echo something, the single quotes will literally echo what you have between them, while the double quotes will evaluate variables between them and output the value of the variable.
如果您指的是当您回显某些内容时会发生什么,单引号将直接回显它们之间的内容,而双引号将评估它们之间的变量并输出变量的值。
For example, this
例如,这个
#!/bin/shMYVAR=sometextecho "double quotes gives you $MYVAR"echo 'single quotes gives you $MYVAR'
will give this:
会给这个:
double quotes gives you sometextsingle quotes gives you $MYVAR
#3
The accepted answer is great. I am making a table that helps in quick comprehension of the topic. The explanation involves a simple variable a
as well as an indexed array arr
.
接受的答案很棒。我正在制作一个有助于快速理解主题的表格。解释涉及一个简单的变量a和一个索引数组arr。
If we set
如果我们设定
a=apple # a simple variablearr=(apple) # an indexed array with a single element
and then echo
the expression in the second column, we would get the result / behavior shown in the third column. The fourth column explains the behavior.
然后在第二列中回显表达式,我们将得到第三列中显示的结果/行为。第四列解释了这种行为。
# | 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 quoting13 | "`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 quoting18 | "$'\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---+-------------+-------------+--------------------------------------------------------------------
See also:
- ANSI-C quoting with
$''
- GNU Bash Manual - Locale translation with
$""
- GNU Bash Manual - A three-point formula for quotes
ANSI-C引用$'' - GNU Bash手册
带有$“”的语言环境翻译 - GNU Bash手册
报价的三点公式
#4
Others explained very well and just want to give with simple examples.
其他人解释得很好,只想用简单的例子给出。
Single quotes can be used around text to prevent the shell from interpreting any special characters. Dollar signs, spaces, ampersands, asterisks and other special characters are all ignored when enclosed within single quotes.
可以在文本周围使用单引号来防止shell解释任何特殊字符。当用单引号括起来时,美元符号,空格,&符号,星号和其他特殊字符都会被忽略。
$ echo 'All sorts of things are ignored in single quotes, like $ & * ; |.'
It will give this:
它会给出这样的:
All sorts of things are ignored in single quotes, like $ & * ; |.
The only thing that cannot be put within single quotes is a single quote.
唯一不能放在单引号内的是单引号。
Double quotes act similarly to single quotes, except double quotes still allow the shell to interpret dollar signs, back quotes and backslashes. It is already known that backslashes prevent a single special character from being interpreted. This can be useful within double quotes if a dollar sign needs to be used as text instead of for a variable. It also allows double quotes to be escaped so they are not interpreted as the end of a quoted string.
双引号的行为类似于单引号,除了双引号仍允许shell解释美元符号,后引号和反斜杠。众所周知,反斜杠可以防止解释单个特殊字符。如果需要将美元符号用作文本而不是变量,则在双引号内这可能很有用。它还允许对双引号进行转义,因此它们不会被解释为带引号的字符串的结尾。
$ echo "Here's how we can use single ' and double \" quotes within double quotes"
It will give this:
它会给出这样的:
Here's how we can use single ' and double " quotes within double quotes
It may also be noticed that the apostrophe, which would otherwise be interpreted as the beginning of a quoted string, is ignored within double quotes. Variables, however, are interpreted and substituted with their values within double quotes.
还可以注意到,撇号(否则将被解释为带引号的字符串的开头)在双引号内被忽略。但是,变量被解释并用双引号内的值代替。
$ echo "The current Oracle SID is $ORACLE_SID"
It will give this:
它会给出这样的:
The current Oracle SID is test
Back quotes are wholly unlike single or double quotes. Instead of being used to prevent the interpretation of special characters, back quotes actually force the execution of the commands they enclose. After the enclosed commands are executed, their output is substituted in place of the back quotes in the original line. This will be clearer with an example.
后引号完全不同于单引号或双引号。后引号实际上强制执行它们所包含的命令,而不是用于防止特殊字符的解释。执行所附的命令后,将替换它们的输出以代替原始行中的后引号。通过一个例子,这将更加清晰。
$ today=`date '+%A, %B %d, %Y'`$ echo $today
It will give this:
它会给出这样的:
Monday, September 28, 2015
#5
Since this is the de facto answer when dealing with quotes in bash
, I'll add upon one more point missed in the answers above, when dealing with the arithmetic operators in the shell.
因为在处理bash中的引号时这是事实上的答案,所以在处理shell中的算术运算符时,我将在上面的答案中添加一个错过的点。
The bash
shell supports two ways do arithmetic operation, one defined by the built-in let
command and the $((..))
operator. The former evaluates an arithmetic expression while the latter is more of a compound statement.
bash shell支持两种算术运算方式,一种由内置let命令和$((..))运算符定义。前者评估算术表达式,而后者更多是复合语句。
It is important to understand that the arithmetic expression used with let
undergoes word-splitting, pathname expansion just like any other shell commands. So proper quoting and escaping needs to be done.
重要的是要理解与let一起使用的算术表达式经历了分词,路径名扩展,就像任何其他shell命令一样。因此需要进行适当的引用和转义。
See this example when using let
使用let时请参阅此示例
let 'foo = 2 + 1'echo $foo3
Using single quotes here is absolutely fine here, as there is no need for variable expansions here, consider a case of
这里使用单引号绝对没问题,因为这里不需要变量扩展,考虑一下
bar=1let 'foo = $bar + 1'
would fail miserably, as the $bar
under single quotes would not expand and needs to be double-quoted as
会失败,因为单引号下的$ bar不会扩大,需要双引号
let 'foo = '"$bar"' + 1'
This should be one of the reasons, the $((..))
should always be considered over using let
. Because inside it, the contents aren't subject to word-splitting. The previous example using let
can be simply written as
这应该是其中一个原因,$((..))应该始终考虑使用let。因为在其内部,内容不受分词的影响。使用let的前一个示例可以简单地写为
(( bar=1, foo = bar + 1 ))
Always remember to use $((..))
without single quotes
Though the $((..))
can be used with double-quotes, there is no purpose to it as the result of it cannot contain a content that would need the double-quote. Just ensure it is not single quoted.
尽管$((..))可以与双引号一起使用,但它没有任何意图,因为它不能包含需要双引号的内容。确保它不是单引号。
printf '%d\n' '$((1+1))'-bash: printf: $((1+1)): invalid numberprintf '%d\n' $((1+1))2printf '%d\n' "$((1+1))"2
May be in some special cases of using the $((..))
operator inside a single quoted string, you need to interpolate quotes in a way that the operator either is left unquoted or under double quotes. E.g. consider a case, when you are tying to use the operator inside a curl
statement to pass a counter every time a request is made, do
可能在一些在单引号字符串中使用$((..))运算符的特殊情况下,您需要以操作符未加引号或双引号的方式插入引号。例如。考虑一个案例,当你想要在curl语句中使用运算符来在每次请求时传递一个计数器时,
curl http://myurl.com --data-binary '{"requestCounter":'"$((reqcnt++))"'}'
Notice the use of nested double-quotes inside, without which the literal string $((reqcnt++))
is passed to requestCounter
field.
注意在内部使用嵌套的双引号,没有它,文字字符串$((reqcnt ++))将传递给requestCounter字段。
#6
There is a clear distinction between the usage of ' '
and " "
.
“和”的用法有明显的区别。
When ' '
is used around anything, there is no "transformation or translation" done. It is printed as it is.
什么时候使用'',没有“转换或翻译”。它是按原样打印的。
With " "
, whatever it surrounds, is "translated or transformed" into its value.
随着“”,无论它包围什么,都被“翻译或转化”为其价值。
By translation/ transformation I mean the following:Anything within the single quotes will not be "translated" to their values. They will be taken as they are inside quotes. Example: a=23
, then echo '$a'
will produce $a
on standard output. Whereas echo "$a"
will produce 23
on standard output.
通过翻译/转换,我的意思是:单引号内的任何内容都不会被“翻译”到它们的值。它们将被引用,因为它们在引号内。例如:a = 23,那么echo'$ a'将在标准输出上产生$ a。回声“$ a”将在标准输出上产生23。