about_Escape_Characters

应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主题

about_Escape_Characters

简短说明

介绍 Windows PowerShell® 中的转义字符并说明其效果。

详细说明

转义字符用于将特殊解释分配给遵循它的字符。

在 Windows PowerShell 中,转义字符是反引号字符 (`),也称为重音符号 (ASCII 96)。转义字符可用于指示文本字符、行继续符和特殊字符。

在对其他程序的调用中,你可以使用停止分析符号 (--%),而不是使用转义字符防止 Windows PowerShell 错误解释程序参数。Windows PowerShell 3.0 中引入了停止分析符号。

对变量进行转义

当转义字符在某个变量前面时,它将防止值替换为变量。

例如:

        PS C:\>$a = 5
        PS C:\>"The value is stored in $a."
        The value is stored in 5.

        PS C:\>$a = 5
        PS C:\>"The value is stored in `$a."
        The value is stored in $a.

对引号进行转义

当转义字符在双引号前面时,Windows PowerShell 将双引号解释为字符,而不是字符串分隔符。

        PS C:\> "Use quotation marks (") to indicate a string."
        Unexpected token ')' in expression or statement.
        At line:1 char:25
        + "Use quotation marks (") <<<<  to indicate a string."

        PS C:\> "Use quotation marks (`") to indicate a string."
        Use quotation marks (") to indicate a string.

使用行继续符

转义字符告诉 Windows PowerShell 该命令在下一行上继续。

例如:

      PS C:\> Get-Process `
      >> PowerShell

      Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
      -------  ------    -----      ----- -----   ------     -- -----------
          340       8    34556      31864   149     0.98   2036 PowerShell
    

使用特殊字符

当在引号内使用时,转义字符指示向命令分析程序提供指令的特殊字符。

以下特殊字符由 Windows PowerShell 识别:

        `0    Null
        `a    Alert
        `b    Backspace
        `f    Form feed
        `n    New line
        `r    Carriage return
        `t    Horizontal tab
        `v    Vertical tab

例如:

        PS C:\> "12345678123456781`nCol1`tColumn2`tCol3"
        12345678123456781
        Col1    Column2 Col3

有关详细信息,请键入:

Get-Help about_Special_Characters      

停止分析符号

在调用其他程序时,你可以使用停止分析符号 (--%) 防止 Windows PowerShell 生成错误或错误解释程序参数。停止分析符号是在程序调用中使用转义字符的替代方法。Windows PowerShell 3.0 中引入了此功能。

例如,以下命令在 Icacls 命令中使用停止分析符号:

        icacls X:\VMS --% /grant Dom\HVAdmin:(CI)(OI)F

有关停止分析符号的详细信息,请参阅 about_Parsing。

另请参阅

about_Quoting_Rules