How to use variables in Azure CLI commands
In addition to specifying values directly in a command, you can provide values in several ways:
- Use shell variables
- Set a subscription for use in multiple commands
- Create default values for some parameters
This article discusses various ways to specify values in Azure CLI commands.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Use shell variables
Azure CLI runs in a shell. This article uses Bash. For information about other shells, see Use Azure CLI effectively. You can use variables in Bash to pass values for parameters to commands. Using variables with the Azure CLI also allows reuse of commands, either piecemeal or in scripts.
This example creates a new storage disk of the same type as the storage disk on an existing virtual machine.
# Bash script
# Assign values to variables
# Get a value for a variable based on an existing virtual machine
osType=$(az vm get-instance-view --resource-group $MyResourceGroup \
--name $vmName --subscription "$MySubscription" \
--query 'storageProfile.osDisk.osType' --output tsv)
# Create a disk of the same type by using the variable value
az disk create --resource-group $MyResourceGroup --name DestinationDisk --size-gb 20 --os-type $osType
This example shows how to assign values to variables that are reused, like MyResourceGroup and osType. The command az vm get-instance-view combined with the query
storageProfile.osDisk.osType returns the disk's OS type. Wrapping the command with
$() assigns the command's return value to
osType. To learn more about
--query and JMESPath queries see How to query Azure CLI command output using a JMESPath query.
When you assign a value to a variable from another command, be sure that the command uses a compatible output format. The az vm get-instance-view command uses the
tsv output format. This option returns values without extra formatting, keys, or other symbols. Some output formats include structure or characters like quotation marks. For more information, see Output formats for Azure CLI commands.
In this example, the MySubscription variable must be in quotation marks. The value of the variable contains spaces, which the command can't parse. If you work only with subscription IDs, you don't need to use quotation marks.
Set a subscription
Many commands require a specific subscription. Azure resources exist in resource groups, which exist in subscriptions. Azure CLI uses a default subscription when you are in a session. To see your current subscription value, run the az account show command:
az account show --output table
az account set --subscription "My Demos"
After you set your subscription, you can omit
--Subscription parameter. For more information, see Use Azure subscriptions with Azure CLI.
Create default values
You can set values for some parameters by using the az config set command. This example sets a default resource group:
az config set defaults.group=ContosoRGforVM
After running this command, you can run the following command to create a storage account in the ContosoRGforVM resource group:
az storage account create --name storage135 --location eastus --sku Standard_LRS
Notice that there's no resource group specified in the command. For more information, see Set a default resource group.
Commands getting values for parameters in different ways can be confusing. If a command gives an unexpected result, such as not being able to find a resource group, there may be a default value.
If you encounter an error, run the command again with the parameter and value specified. An explicit value for a parameter always takes precedence over other options.
You can specify values for several parameters this way. For more information, see Azure CLI configuration.
Clean up resources
If you created resources to try any of the commands in this article, you can remove them by using the az group delete command:
az group delete --name ContosoRGforVM
az group delete --name ContosoStorageRG
This command removes the group and all the resources that it contains at once.