Bash中单引号和双引号的区别
在Bash中,单引号(Bash中单引号和双引号的区别,bash,shell,syntax,quotes,Bash,Shell,Syntax,Quotes,在Bash中,单引号(”)和双引号(”)之间有什么区别?单引号不会插入任何内容,但双引号会。例如:变量、反勾号、某些\转义等 例如: $ echo "$(echo "upg")" upg $ echo '$(echo "upg")' $(echo "upg") Bash手册中有这样一句话: 将字符括在单引号(”)中会保留引号中每个字符的文字值。单引号不能出现在单引号之间,即使前面有反斜杠 将字符括在双引号中(“)将保留引号中所有字符的文字值,但$、`、\除外,并且,当启用历史扩展时,!。字
”
)和双引号(”
)之间有什么区别?单引号不会插入任何内容,但双引号会。例如:变量、反勾号、某些\
转义等
例如:
$ echo "$(echo "upg")"
upg
$ echo '$(echo "upg")'
$(echo "upg")
Bash手册中有这样一句话:
将字符括在单引号(”
)中会保留引号中每个字符的文字值。单引号不能出现在单引号之间,即使前面有反斜杠
将字符括在双引号中(“
)将保留引号中所有字符的文字值,但$
、`
、\
除外,并且,当启用历史扩展时,!
。字符$
和`
在双引号中保留其特殊含义(请参阅)。反斜杠只有在后跟以下字符之一时才保留其特殊含义:$
,`
,“
、\
或换行符。在双引号中,后跟这些字符之一的反斜杠将被删除。没有特殊含义的反斜杠前面的字符将保持不变。双引号可以通过在双引号中加反斜杠来引引。如果启用,将执行历史扩展,除非出现在双引号中的n!
使用反斜杠转义。不删除!
前面的反斜杠
特殊参数*
和@
在双引号中具有特殊意义(请参阅)
如果您指的是当您回送某个内容时发生的情况,则单引号将直接回送它们之间的内容,而双引号将计算它们之间的变量并输出变量的值 比如说这个,
#!/bin/sh
MYVAR=sometext
echo "double quotes gives you $MYVAR"
echo 'single quotes gives you $MYVAR'
将给出:
double quotes gives you sometext
single quotes gives you $MYVAR
All sorts of things are ignored in single quotes, like $ & * ; |.
Here's how we can use single ' and double " quotes within double quotes
The current Oracle SID is test
Monday, September 28, 2015
非常好。我正在制作一个表格,帮助快速理解主题。解释包括一个简单变量a
以及一个索引数组arr
如果我们开始
a=apple # a simple variable
arr=(apple) # an indexed array with a single element
然后,echo
第二列中的表达式,我们将得到第三列中显示的结果/行为。第四列解释了该行为
#
表情
结果
评论
1.
“$a”
apple
变量在中展开。
2.
“$a”
$a
变量未在'
3.
“$a”
“苹果”
”
在中没有特殊含义
4.
“$a”
“$a”
”
在”
5.
\''
无效
无法在'
中转义'
;使用'
或$'\'
(ANSI-C引用)
6.
“红色$arocks”
红色
$arocks
不展开$a
;使用${a}岩石
保存$a
7.
“红苹果$”
redapple$
$
后跟无变量名的计算结果为$
8.
“\”
\”
\
在'
9
“\”
\'
\'
在''
内部解释,但对'
10
“\”
“
\“
在”
11
“*”
*
glob在“”
或“”
12
“\t\n”
\t\n
\t
和\n
在“
或”
中没有特殊含义;使用ANSI-C引用
13
“`echo hi`”
hi
`
和$()
在“
中进行计算(反引号保留在实际输出中)
14
''echo hi`'
`echo hi`
`
和$()
不在'
内计算(反引号保留在实际输出中)
15
'${arr[0]}'
${arr[0]}
在'
16
“${arr[0]}”
apple
数组访问在内部工作。“
17
$'$a\'
$a'
单引号可以在ANSI-C引号内转义
18
“$”\t“
$'\t'
ANSI-C引用不在“
19
“!cmd”
!cmd
在'
20
“!cmd”
cmd args
扩展到最新的命令匹配“cmd”
21
$”!cmd'
!cmd
ANSI-C引号中忽略历史扩展字符“!”
其他人解释得很好,只想举一些简单的例子
单引号可用于文本周围,以防止shell解释任何特殊字符。当包含在单引号内时,美元符号、空格、符号、星号和其他特殊字符都将被忽略
$ echo 'All sorts of things are ignored in single quotes, like $ & * ; |.'
$ echo "The current Oracle SID is $ORACLE_SID"
它将提供:
double quotes gives you sometext
single quotes gives you $MYVAR
All sorts of things are ignored in single quotes, like $ & * ; |.
Here's how we can use single ' and double " quotes within double quotes
The current Oracle SID is test
Monday, September 28, 2015
唯一不能放在单引号内的是单引号
双引号的作用类似于单引号,但双引号仍然允许shell解释美元符号、反引号和反斜杠。众所周知,反斜杠阻止解释单个特殊字符。如果需要将美元符号用作文本替代符,这在双引号中非常有用变量的ead。它还允许双引号转义,因此它们不会被解释为带引号字符串的结尾
$ echo "Here's how we can use single ' and double \" quotes within double quotes"
它将提供:
double quotes gives you sometext
single quotes gives you $MYVAR
All sorts of things are ignored in single quotes, like $ & * ; |.
Here's how we can use single ' and double " quotes within double quotes
The current Oracle SID is test
Monday, September 28, 2015
还可能注意到,撇号在双引号中被忽略,否则会被解释为带引号字符串的开头