Muokkaa

Jaa


Quoting differences between scripting languages

When you work with Azure CLI commands, be aware of how your scripting language uses quotation marks and escapes characters. If you support scripts used in different shells, understanding quoting differences saves you valuable development hours.

To avoid unanticipated results with parameter values containing single or double quotes, or escape characters, here are a few suggestions:

White spaces and quotation marks

  • If you provide a parameter value that contains white space, wrap the value in quotation marks.

  • In Bash and PowerShell, if your variable value contains single quotes, wrap the value in double quotes, and vice-versa.

  • In Bash, double quotes that are escaped, are treated as part of the string.

  • In Windows Command Prompt, quotes inside variable values are treated as part of the value.

Here are a few examples:

# 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 output for the correct examples is as follows:

my string ' ' wrapped in double quotes
my string " " wrapped in single quotes
my string with escaped " " double quotes wrapped in double quotes

If you want the quotes included in the output, escape the variable like this: 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 strings

  • Use single quotes to preserve the content inside a JSON string. Single quotes are necessary when supplying inline JSON values. For example, this JSON is correct in both Bash and PowerShell: '{"key": "value"}'.

  • If your command runs at a Windows Command Prompt, you must use double quotes. The equivalent of the above JSON string in Cmd.exe is "{"key":"value"}" .

  • If the JSON value contains double quotes, you must escape them.

  • When working with JSON parameter values, consider using Azure CLI's @<file> convention and bypass the shell's interpretation mechanisms.

    az ad app create --display-name myName --native-app --required-resource-accesses @manifest.json
    

Here are the accepted JSON format patterns for Bash, PowerShell and Cmd:

Use Bash's clear command to remove console output between tests.

# Correct in Bash
az '{"key":"value"}' --debug
>> Command arguments: ['{"key":"value"}', '--debug']

az "{\"key\":\"value\"}" --debug
>> Command arguments: ['{"key":"value"}', '--debug']

These next two examples are incorrect as quotes and spaces are interpreted by Bash.

Incorrect format Problem Console output
az {"key":"value"} --debug Missing single quotes or escape characters Command arguments: ['{key:value}', '--debug']
az {"key": "value"} --debug Missing single quotes or escape characters, and contains extra space Command arguments: ['{key:', 'value}', '--debug']

Learn more about working with JSON parameter values in How to use shorthand syntax with Azure CLI.

Empty strings

  • In PowerShell, if your value is an empty quotes string (''), use '""'.

  • In Bash or PowerShell, if your value is an empty quotes string (''), use "''".

    # Correct in Bash
    myVariable="''"
    
    # Correct in PowerShell
    $myVariable = "''"
    $myVariable = '""'
    

Space-separated values

Some Azure CLI commands take a list of space separated values. If the key name or value contains spaces, wrap the whole pair: "my key=my value". For example:

az web app config app settings set --resource-group myResourceGroup --name myWebAppName --settings "client id=id1" "my name=john"

When a CLI parameter states that it accepts a space-separated list, one of two formats is expected:

  • Example of unquoted, space-separated list: --parameterName firstValue secondValue

  • Example of quoted space-separated list: --parameterName "firstValue" "secondValue"

This example is a string with a space in it. It isn't a space-separated list: --parameterName "firstValue secondValue"

Special characters

There are special characters in the PowerShell scripting language, such as at @. To run Azure CLI in PowerShell, add ` before the special character to escape it. You can also enclose the value in single or double quotes "/".

# 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

Hyphen characters

If a parameter's value begins with a hyphen, Azure CLI tries to parse it as a parameter name. To parse it as value, use = to concatenate the parameter name and value: --password="-VerySecret".

The --query parameter

When you use the --query parameter with a command, some characters of JMESPath need to be escaped in the shell.

These three commands are correct and equivalent in Bash:

az version --query '"azure-cli"'
az version --query \"azure-cli\"
az version --query "\"azure-cli\""

Here are two examples of incorrect commands in 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'

For more example comparisons between Bash, PowerShell, and Cmd, see Query Azure CLI command output.

The --debug parameter

The best way to troubleshoot a quoting issue is to run the command with the --debug flag. This flag reveals the actual arguments received by the Azure CLI in Python's syntax.

For more information on troubleshooting Azure CLI commands with --debug, see Troubleshooting Azure CLI.

Scripting language rules

Here are quick links to scripting language rules as published by their respective organizations:

Note

Due to a known issue in PowerShell, some extra escaping rules apply. For more information, see Considerations for running the Azure CLI in a PowerShell scripting language.

See also

Find many more scripting language comparisons in these articles: