一直困扰我的简单问题:在powershell中,我可以这样定义字符串:
$s1 = "Boogety boo"
或
$s2 = '.net rocks'
翻译有什么不同吗?
发布于 2011-05-12 00:19:17
双引号允许变量扩展,而单引号不允许:
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版,但原理仍然是一样的)
发布于 2017-01-12 09:35:07
这不是试图成为一个更好的答案。只是另一种说法。
撇号和引号之间的变量扩展与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
https://stackoverflow.com/questions/5967407
复制相似问题