Specifying values 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
  • Use persistent values for some parameters

This article discusses various ways to specify values in Azure CLI commands.

Prerequisites

  • Use Azure Cloud Shell using the bash environment.

    Launch Cloud Shell in a new window

  • If you prefer, install the Azure CLI to run CLI reference commands.

    • If you're using a local install, sign in with Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. See Sign in with Azure CLI for additional sign-in options.
    • When you're prompted, install Azure CLI extensions on first use. For more information about extensions, see Use extensions with Azure CLI.
    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

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 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.

# Assign values to variables
MyResourceGroup=ContosoRGforVM
MySubscription="Contoso subscription"
vmName=VM01

# 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 assigns values to variables that are reused, like MyResourceGroup. A command gets a value to assign to osType.

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. Its value 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

You might have access to only one subscription. For more information, see Use Azure subscriptions with Azure CLI You can use the az account set command to set your current subscription:

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.

Tip

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.

Use persistent values

Persisted parameter values allow you to specify a value only once. If you're doing several related actions in a resource group, you don't have to specify that group repeatedly.

Run this command to persist a parameter value:

az config param-persist on

After turning on persistence, create a resource group:

az group create --name ContosoStorageRG --location eastus

As long as persistence is on, your can leave the --resource-group parameter out of future commands. The following command creates a storage account in the ContosoStorageRG resource group:

az storage account create --name storage135 --location eastus --sku Standard_LRS

For more information, see Azure CLI persisted parameter.

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.

You can remove the persistent parameters by running the az config param-persist delete command:

az config param-persist delete --all

Next steps