PowerShell中单引号(’)和双引号(“)的区别?
PowerShell中单引号(’)和双引号(“)之间没有区别。这与Python之类的编程语言类似。我们通常使用这两种引号来打印语句。
示例
PS C:\> Write-Output 'This will be printed using Single quote' This will be printed using Single quote PS C:\> Write-Output "This will be printed using double quote" This will be printed using double quote
但是,当我们评估任何表达式或打印变量时,就会有明显的区别。
$date = Get-Date Write-Output 'Today date is : $date' Today date is : $date Write-Output "Today date is : $date" Today date is : 11/02/2020 08:13:06
您可以从上面的示例中看到,单引号无法打印变量输出,而是打印变量的名称,而双引号可以打印变量的输出。即使我们尝试评估变量名称,也无法使用单引号来完成。
示例
PS C:\> Write-Output 'Today date is : $($date)' Today date is : $($date)
乘法运算的另一个示例:
PS C:\> Write-Output "Square of 4 : $(4*4)" Square of 4 : 16 PS C:\> Write-Output 'square of 4 : $(4*4)' square of 4 : $(4*4)
以使用数组打印多个语句为例。
示例
$name = 'Charlie' $age = 40 $str = @" New Joinee name is $name Her age is $age "@ $str
输出
New Joinee name is Charlie Her age is 40 He will receive 1200 dollar bonus after 2 years
上面的输出正确打印,但是当我们使用单引号时,它不会打印变量名。
示例
$str = @' New Joinee name is $name Her age is $age He will receive $($age*30) dollar bonus after 2 years '@
输出
New Joinee name is $name Her age is $age He will receive $($age*30) dollar bonus after 2 years
总之,仅使用单引号打印纯文本,而要打印变量和在字符串中评估其他表达式,请在PowerShell中使用双引号。
广告