about_Quoting_Rules
简短说明
介绍在 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.
只有基本变量引用可以直接嵌入到可展开字符串中。 使用数组索引或成员访问的变量引用必须包含在子表达式中。 例如:
"PS version: $($PSVersionTable.PSVersion)"
PS version: 7.2.0
若要将变量名称与字符串中的后续字符分开,请将其括在大括号 ({}
) 。 如果变量名称后跟冒号 (:
) ,则这一点尤其重要。 PowerShell 会考虑 和 :
范围说明符之间的$
所有内容,这通常会导致解释失败。 例如, "$HOME: where the heart is."
引发错误,但 "${HOME}: where the heart is."
按预期工作。
若要防止替换双引号字符串中的变量值,请使用反引号字符 (`
) ,即 PowerShell 转义字符。
在以下示例中,第一个 $i
变量前面的反引号字符阻止 PowerShell 将变量名称替换为其值。
例如:
$i = 5
"The value of `$i is $i."
此命令的输出为:
The value of $i 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.
在字符串中包含引号字符
若要使双引号出现在字符串中,请将整个字符串括在单引号中。 例如:
'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 将引号解释为字符串分隔符。 例如:
"Use a quotation mark (`") to begin a string."
'Use a quotation mark (`") to begin a string.'
由于单引号字符串的内容按字面解释,反引号字符被视为文本字符并显示在输出中。
Use a quotation mark (") to begin a string.
Use a quotation mark (`") to begin a string.
Here-strings
此处字符串的引用规则略有不同。
here 字符串是一个单引号或双引号字符串,周围有 at 符号 (@
) 。 here-string 中的引号按字面解释。
此处字符串:
- 跨多行
- 以开始标记后跟换行符开头
- 以换行符结尾,后跟结束标记
- 包括开始和结束标记之间的每一行作为单个字符串的一部分
与常规字符串一样,变量由双引号的 here-strings 中的值替换。 在单引号的 here 字符串中,变量不会替换为其值。
可以将 here-strings 用于任何文本,但它们对于以下类型的文本特别有用:
- 包含文本引号的文本
- 多行文本,例如 HTML 或 XML 块中的文本
- 脚本或函数文档的帮助文本
here-string 可以具有以下格式之一,其中 <Enter>
表示按 Enter 键时添加的换行符或换行符隐藏字符。
双引号:
@"<Enter>
<string> [string] ...<Enter>
"@
单引号:
@'<Enter>
<string> [string] ...<Enter>
'@
注意
最后一个换行符是结束标记的一部分。 它不会添加到 here-string 中。
here 字符串包含开始标记和结束标记之间的所有文本。 在 here-string 中,所有引号都按字面解释。 例如:
@"
For help, type "get-help"
"@
此命令的输出为:
For help, type "get-help"
使用 here-string 可以简化在命令中使用字符串。 例如:
@"
Use a quotation mark, like ' or ", to begin a string.
"@
此命令的输出为:
Use a quotation mark, like ' or ", 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="http://schemas.microsoft.com/maml/2004/10"
xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10"
xmlns:dev="http://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
。
注意
PowerShell 允许双引号或单引号字符串跨多行,而无需使用 @
here-strings 的语法。 但是,完整的 here-string 语法是首选用法。
可展开字符串的解释
展开的字符串不一定与在控制台中看到的默认输出相同。
集合(包括数组)通过在元素的字符串表示形式之间放置一个空格来转换为字符串。 可以通过设置首选项变量 $OFS
来指定不同的分隔符。 有关详细信息,请参阅 $OFS
首选项变量。
通过调用 ToString()
方法将任何其他类型的实例转换为字符串,该方法可能不会提供有意义的表示形式。 例如:
"hashtable: $(@{ key = 'value' })"
hashtable: System.Collections.Hashtable
若要获取与控制台中的相同输出,请使用子表达式,在其中通过管道连接到 Out-String
。 Trim()
如果要删除任何前导和尾随空行,请应用 方法。
"hashtable:`n$((@{ key = 'value' } | Out-String).Trim())"
hashtable:
Name Value
---- -----
key value
区域性设置影响字符串解释
方法 ToString()
使用当前配置的区域性设置将值转换为字符串。 例如,以下 PowerShell 会话的区域性设置为 de-DE
。 ToString()
当 方法将 的值$x
转换为字符串时,它使用逗号 (,
小数分隔符) 。 此外, ToString()
方法使用德国区域设置的适当格式将日期转换为字符串。
PS> Get-Culture
LCID Name DisplayName
---- ---- -----------
1031 de-DE German (Germany)
PS> $x = 1.2
PS> $x.ToString()
1,2
PS> (Get-Date 2024-03-19).ToString()
19.03.2024 00:00:00
但是,PowerShell 在解释可展开字符串表达式时使用固定区域性。
PS? "$x"
1.2
PS> "$(Get-Date 2024-03-19)"
03/19/2024 00:00:00
将带引号的字符串传递到外部命令
某些本机命令需要包含引号字符的参数。 PowerShell 在将带引号的字符串传递给外部命令之前对其进行解释。 此解释将删除外部引号字符。
有关此行为的详细信息,请参阅 about_Parsing 一文。