问题描述
在 Bash 中,单引号 (''
) 和双引号 (""
) 有什么区别?
In Bash, what are the differences between single quotes (''
) and double quotes (""
)?
推荐答案
单引号不会插入任何内容,但双引号会.例如:变量、反引号、某些 转义等.
Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain escapes, etc.
示例:
$ echo "$(echo "upg")"
upg
$ echo '$(echo "upg")'
$(echo "upg")
Bash 手册是这样说的:
The Bash manual has this to say:
将字符括在单引号 ('
) 中会保留引号内每个字符的字面值.单引号之间不能出现单引号,即使前面有反斜杠.
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.
用双引号括起来的字符 ("
) 保留引号内所有字符的字面值,$
、`
除外, , 并且,当启用历史扩展时,
!
.字符 $
和 `
保留其特殊含义在双引号内(参见 Shell Expansions). 反斜杠仅在后跟以下字符之一时保留其特殊含义:$
, `
, "
, 或换行符.在双引号内,后跟这些字符之一的反斜杠被删除.没有特殊含义的字符前面的反斜杠保持不变.双引号可以通过在双引号前面加上反斜杠来引用.如果启用,将执行历史扩展,除非出现在双引号中的
!
使用反斜杠进行转义.!
前面的反斜杠不会被删除.
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.
特殊参数 *
和 @
在双引号中具有特殊含义(参见 Shell 参数扩展).
The special parameters *
and @
have special meaning when in double quotes (see Shell Parameter Expansion).
这篇关于Bash中单引号和双引号的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!