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中使用双引号。
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿