關於引號規則
簡短描述
描述在PowerShell中使用單引號和雙引號的規則。
詳細描述
引號是用來指定常值字串。 您可以將字串括在單引號 ('
) 或雙引號 ("
) 。
引號也可用來建立 here-string。 here-string 是單引號或雙引號字串,其中會以常值方式解譯引號。 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-string 是單引號或雙引號字串,其中會以常值方式解譯引號。 here-string 可以跨越多行。 這裡字串中的所有行都會解譯為字串,即使它們未以引號括住也一樣。
如同一般字串,變數會以雙引弧括住的here-strings取代變數。 在以單引弧括住的 here-strings 中,變數不會由其值取代。
您可以針對任何文字使用 here-strings,但它們特別適用於下列類型的文字:
- 包含常值引號的文字
- 多行文字,例如 HTML 或 XML 中的文字
- 文稿或函式檔的說明文字
here-string 可以有下列其中一種格式,其中 <Enter>
代表按下 ENTER 鍵時新增的換行字元或換行符。
雙引號:
@"<Enter>
<string> [string] ...<Enter>
"@
單引號:
@'<Enter>
<string> [string] ...<Enter>
'@
任一格式的右引號都必須是行中的第一個字元。
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 會將 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>
"@
這裡字串也是 Cmdlet 輸入 ConvertFrom-StringData
的便利格式,可將 here-strings 轉換成哈希表。
如需詳細資訊,請參閱ConvertFrom-StringData
。