简单的问题一个字符串,一直缠着我:在PowerShell中,我可以定义字符串,像这样:什么是单引号和双引号之间的区别定义在PowerShell中
$s1 = "Boogety boo"
或
$s2 = '.net rocks'
口译员有什么不同吗?
简单的问题一个字符串,一直缠着我:在PowerShell中,我可以定义字符串,像这样:什么是单引号和双引号之间的区别定义在PowerShell中
$s1 = "Boogety boo"
或
$s2 = '.net rocks'
口译员有什么不同吗?
双引号允许变量扩展,而单引号不要:
PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor
来源:http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(我知道1.0版本,但原则仍然是相同的)
这是不是想成为更好的答案。只是另一种说法。
撇号和引号之间的变量扩展与UNIX shell(sh,ksh,bash)上的相同。使用撇号将按原样使用字符串,而不处理任何转义。
PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x
PS C:\Users\lit> Write-Output "now${x}is"
now is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word
太棒了。感谢那! – 2011-05-11 16:54:18
onteria_引用的变量扩展也称为“[插值]”(http://www.powershellpro.com/powershell-tutorial-introduction/variables-arrays-hashes/)“。你也可以把插值看作“连接的一个专用实例”,正如[Wikipedia](http://en.wikipedia.org/wiki/Variable_(computer_science)#Interpolation)提出的那样。 – Stisfa 2011-10-06 01:14:13
另请参见:[字符串和字符串中的可变扩展](http://blogs.msdn.com/b/powershell/archive/2006/07/15/variable-expansion-in-strings-and-herestrings.aspx )由Jeffrey Snover撰写,其中详细介绍了如何在字符串中使用变量。 – 2011-11-20 14:21:59