當您使用 Azure CLI 命令時,請注意您的腳本語言如何使用引號和轉義字元。 如果您支持在不同 shell 中使用的腳本,了解引用符號差異可以節省寶貴的開發時間。
若要使用包含單引號或雙引號或逸出字元的參數值來避免非預期的結果,以下是一些建議:
空白和引號
如果您提供包含空格符的參數值,請以引弧括住值。
在Bash和PowerShell中,如果您的變數值包含單引號,請以雙引號括住值,反之亦然。
在Bash中,逸出的雙引號會被視為字串的一部分。
在 Windows 命令提示字元中,變數值內的引號會視為值的一部分。
以下是一些範例:
# Correct
myVariable="my string ' ' wrapped in double quotes"
myVariable='my string " " wrapped in single quotes'
myVariable="my string with escaped \" \" double quotes wrapped in double quotes"
# Wrong, escaped single quotes in Bash are not treated as part of the string
myVariable='my value with escaped \' \' single quotes wrapped in single quotes'
# after each example ...
echo $myVariable
正確範例的 Bash 輸出如下所示:
my string ' ' wrapped in double quotes
my string " " wrapped in single quotes
my string with escaped " " double quotes wrapped in double quotes
如果您想要輸出中包含的引號,請逸出變數,如下所示: echo \"$myVariable\"。
echo \"$myVariable\"
"my string ' ' wrapped in double quotes"
echo \'$myVariable\'
'my string " " wrapped in single quotes'
echo \"$myVariable\"
"my string with escaped " " double quotes wrapped in double quotes"
JSON 字串
使用單引號來保留 JSON 字串內的內容。 提供內嵌 JSON 值時,需要單引號。 例如,Bash 和 PowerShell 中的這個 JSON 正確:
'{"key": "value"}'。如果您的命令在 Windows 命令提示字元中執行,您必須使用雙引號。 Cmd.exe 中上述的 JSON 字串的對應物是
"{"key":"value"}"。如果 JSON 值包含雙引號,您必須逸出它們。
使用 JSON 參數值時,請考慮使用 Azure CLI 的
@<file>慣例,並略過 Shell 的解譯機制。az ad app create --display-name myName --native-app --required-resource-accesses @manifest.json
以下是 Bash、PowerShell 和 Cmd 可接受的 JSON 格式模式:
使用 Bash 的 clear 命令來移除測試之間的控制台輸出。
# Correct in Bash
az '{"key":"value"}' --debug
>> Command arguments: ['{"key":"value"}', '--debug']
az "{\"key\":\"value\"}" --debug
>> Command arguments: ['{"key":"value"}', '--debug']
接下來的兩個範例 不正確 ,因為Bash會解譯引號和空格。
| 格式不正確 | 問題 | 主控台輸出 |
|---|---|---|
| az {“key”:“value”} --debug | 遺漏單引號或逸出字元 | 命令參數:['{key: value}', '--debug'] |
| az {“key”: “value”} --debug | 遺漏單引號或逸出字元,並包含額外的空格 | 命令參數:['{key:', 'value}', '--debug'] |
若要深入瞭解如何使用 JSON 參數值, 請參閱如何搭配 Azure CLI 使用速記語法。
空字串
在 PowerShell 中,如果您的值是空引號字串串 (
''),請使用'""'。在 Bash 或 PowerShell 中,如果您的值是空引號字串 (
''),請使用"''"。# Correct in Bash myVariable="''" # Correct in PowerShell $myVariable = "''" $myVariable = '""'
空格分隔值
某些 Azure CLI 命令會採用空格分隔值的清單。 如果鍵名稱或值包含空格,請括住整個配對:"my key=my value"。 例如:
az web app config app settings set --resource-group myResourceGroup --name myWebAppName --settings "client id=id1" "my name=john"
當 CLI 參數指出它接受以空白分隔的清單時,預期有兩種格式之一:
未加上批注、以空白分隔的清單範例:
--parameterName firstValue secondValue以引號分隔的清單範例:
--parameterName "firstValue" "secondValue"
此範例是字串,其中包含一個空格。 它不是以空格分隔的清單: --parameterName "firstValue secondValue"
特殊字元
PowerShell 文稿語言中有特殊字元,例如 在 @。 若要在PowerShell中執行 Azure CLI,請在特殊字元之前新增 ` 以逸出它。 您也可以以單引號或雙引號 "/"括住值。
# The following three examples will work in PowerShell
--parameterName `@parameters.json
--parameterName '@parameters.json'
--parameterName "@parameters.json"
# This example will not work in PowerShell
--parameterName @parameters.json
連字號
如果參數的值以連字元開頭,Azure CLI 會嘗試將它剖析為參數名稱。 若要將它剖析為值,請使用 = 來串連參數名稱和值: --password="-VerySecret"。
--query 參數
當您搭配 --query 命令使用 參數時,必須在殼層中逸出 JMESPath 的某些字元。
這三個命令在Bash中是正確的和對等的:
az version --query '"azure-cli"'
az version --query \"azure-cli\"
az version --query "\"azure-cli\""
以下是 Bash 中 不正確命令 的兩個範例:
# Wrong, as the dash needs to be quoted in a JMESPath query
az version --query azure-cli
az version: error: argument --query: invalid jmespath_type value: 'azure-cli'
# Wrong, as the dash needs to be quoted in a JMESPath query, but quotes are interpreted by Bash
az version --query "azure-cli"
az version: error: argument --query: invalid jmespath_type value: 'azure-cli'
如需 Bash、PowerShell 和 Cmd 之間的更多範例比較,請參閱 查詢 Azure CLI 命令輸出。
--debug 參數
對引用問題進行疑難解答的最佳方式是使用 --debug 旗標執行 命令。 此旗標會顯示 Azure CLI 在 Python 語法中收到的實際自變數。
如需有關 Azure CLI 命令 --debug 疑難排解的詳細資訊,請參閱 Azure CLI 的疑難排解。
腳本語言規則
以下是其個別組織所發佈的文本語言規則快速連結:
- Bash 腳本語言: Bash 引用規則
- PowerShell 腳本語言: PowerShell 報價規則
- Windows 命令提示字元:使用說明:Windows 命令列上的逃逸字元、分隔符和引號
備註
由於 PowerShell 中已知的問題,一些額外的跳脫規則會被套用。 如需詳細資訊,請參閱使用 PowerShell 腳本語言執行 Azure CLI 的考慮。
另請參閱
在這些文章中尋找更多文稿語言比較: