关于引号规则
简短说明
介绍在 PowerShell 中使用单引号和双引号的规则。
详细说明
引号用于指定文本字符串。 可以将字符串括在单引号 () '
或双引号 ("
) 。
引号还用于创建 here 字符串。 here 字符串是单引号或双引号字符串,其中引号按字面解释。 here 字符串可以跨多行。 here-string 中的所有行都解释为字符串,即使它们未用引号引起来。
在远程计算机的命令中,引号定义远程计算机上运行的命令的各个部分。 在远程会话中,引号还确定是先在本地计算机上还是在远程计算机上解释命令中的变量。
单引号和双引号字符串
将字符串括在双引号 (双引号字符串) 时,在字符串传递给命令进行处理之前,前面带有美元符号 $
() 的变量名称将替换为变量的值。
例如:
$i = 5
"The value of $i is $i."
此命令的输出为:
The value of 5 is 5.
此外,在双引号字符串中,计算表达式,并将结果插入字符串中。 例如:
"The value of $(2+3) is 5."
此命令的输出为:
The value of 5 is 5.
将字符串括在单引号 (单引号字符串) 时,该字符串将完全在键入时传递给命令。 不执行替换。 例如:
$i = 5
'The value of $i is $i.'
此命令的输出为:
The value $i is $i.
同样,不会计算单引号字符串中的表达式。 它们被解释为文本。 例如:
'The value of $(2+3) is 5.'
此命令的输出为:
The value of $(2+3) is 5.
若要防止替换双引号字符串中的变量值,请使用反引号字符 (`
) (ASCII 96) ,即 PowerShell 转义字符。
在以下示例中,第一个$i变量前面的反引号字符阻止 PowerShell 将变量名称替换为其值。 例如:
$i = 5
"The value of `$i is $i."
此命令的输出为:
The value $i is 5.
若要使双引号出现在字符串中,请将整个字符串括在单引号中。 例如:
'As they say, "live and learn."'
此命令的输出为:
As they say, "live and learn."
还可以将单引号字符串括在双引号字符串中。 例如:
"As they say, 'live and learn.'"
此命令的输出为:
As they say, 'live and learn.'
或者,在双引号短语的两边加引号。 例如:
"As they say, ""live and learn."""
此命令的输出为:
As they say, "live and learn."
若要在单引号字符串中包含单引号,请使用第二个连续的单引号。 例如:
'don''t'
此命令的输出为:
don't
若要强制 PowerShell 按字面解释双引号,请使用反引号字符。 这可以防止 PowerShell 将引号解释为字符串分隔符。 例如:
PS> "Use a quotation mark (`") to begin a string."
Use a quotation mark (") to begin a string.
PS> 'Use a quotation mark (`") to begin a string.'
Use a quotation mark (`") to begin a string.
由于单引号字符串的内容按字面解释,反引号字符被视为文本字符并显示在输出中。
HERE-STRINGS
此处字符串的引用规则略有不同。
here 字符串是单引号或双引号字符串,其中引号按字面解释。 here 字符串可以跨多行。 here-string 中的所有行都解释为字符串,即使它们未用引号引起来。
与常规字符串一样,变量由双引号的 here-strings 中的值替换。 在单引号的 here-strings 中,变量不会替换为其值。
可以将 here-strings 用于任何文本,但它们对于以下类型的文本特别有用:
- 包含文本引号的文本
- 多行文本,例如 HTML 或 XML 中的文本
- 脚本或函数文档的帮助文本
here-string 可以具有以下格式之一,其中 <Enter>
表示按 Enter 键时添加的换行符或换行符隐藏字符。
双引号:
@"<Enter>
<string> [string] ...<Enter>
"@
单引号:
@'<Enter>
<string> [string] ...<Enter>
'@
在任一格式中,右引号必须是行中的第一个字符。
here 字符串包含两个隐藏字符之间的所有文本。 在 here-string 中,所有引号都按字面解释。 例如:
@"
For help, type "get-help"
"@
此命令的输出为:
For help, type "get-help"
使用 here-string 可以简化在命令中使用字符串。 例如:
@"
Use a quotation mark (') to begin a string.
"@
此命令的输出为:
Use a quotation mark (') to begin a string.
在单引号 here-strings 中,变量按字面解释并准确重现。 例如:
@'
The $profile variable contains the path
of your PowerShell profile.
'@
此命令的输出为:
The $profile variable contains the path
of your PowerShell profile.
在双引号的 here-strings 中,变量将替换为其值。 例如:
@"
Even if you have not created a profile,
the path of the profile file is:
$profile.
"@
此命令的输出为:
Even if you have not created a profile,
the path of the profile file is:
C:\Users\User1\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.
Here-string 通常用于向变量分配多行。 例如,下面的 here-string 将 XML 页分配给 $page 变量。
$page = [XML] @"
<command:command xmlns:maml="https://schemas.microsoft.com/maml/2004/10"
xmlns:command="https://schemas.microsoft.com/maml/dev/command/2004/10"
xmlns:dev="https://schemas.microsoft.com/maml/dev/2004/10">
<command:details>
<command:name>
Format-Table
</command:name>
<maml:description>
<maml:para>Formats the output as a table.</maml:para>
</maml:description>
<command:verb>format</command:verb>
<command:noun>table</command:noun>
<dev:version></dev:version>
</command:details>
...
</command:command>
"@
Here-strings 也是输入 cmdlet 的 ConvertFrom-StringData
便捷格式,用于将 here-string 转换为哈希表。
有关详细信息,请参阅 ConvertFrom-StringData
。