powershell中单引号和双引号定义字符串有什么区别
一直困扰我的简单问题:在 powershell 中,我可以像这样定义字符串:
$s1 = "Boogety boo"
或者
$s2 = '.net rocks'
解释器有什么区别吗?
Simple questions that's been bugging me: In powershell, I can define strings like so:
$s1 = "Boogety boo"
or
$s2 = '.net rocks'
Is there a difference to the interpreter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
双引号允许变量扩展,而单引号则不允许:
来源:http://www.techotopia。 com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(我知道1.0版但原理还是一样的)
Double quotes allow variable expansion while single quotes do not:
Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(I know version 1.0 but the principle is still the same)
这并不是试图成为更好的答案。只是另一种说法。
撇号和引号之间的变量扩展与 UNIX shell(sh、ksh、bash)上的相同。使用撇号将按原样获取字符串,而不处理任何转义。
This is not trying to be a better answer. Just another way to say it.
The variable expansion between apostrophes and quotes are the same as on UNIX shells (sh, ksh, bash). Using apostrophes will take the character string as-is, without processing any escapes.
这个问题在 about_Quoting_Rules< 中有直接答案/a> PowerShell 文档的文章:
换句话说,如果您希望字符串保持原样,请使用单引号。如果要插入变量 (
$myVariable
)、命令执行结果和其他评估 ($($myList -join ', ')
) 或特殊值,请使用双引号字符(`r
、`n
、`t
、`a
等)。This question has a direct answer in the about_Quoting_Rules article of the PowerShell docs:
In other words, use single quotes if you want your string to remain as written. Use double quotes, if you want to insert variables (
$myVariable
), results of command executions and other evaluations ($($myList -join ', ')
) or special characters (`r
,`n
,`t
,`a
, etc.).