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中使用雙引號。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP