How to use variables in Azure CLI commands (2024)

  • Article

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.

Prerequisites

  • Use the Bash environment in Azure Cloud Shell. For more information, see Quickstart for Bash in Azure Cloud Shell.

    How to use variables in Azure CLI commands (1)

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

    • 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 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
  • PowerShell
# Bash script# Assign values to variablesMyResourceGroup=ContosoRGforVMMySubscription="Contoso subscription"vmName=VM01# Get a value for a variable based on an existing virtual machineosType=$(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 valueaz 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

You might have access to only one subscription. For more information, see Use Azure subscriptions with Azure CLIYou 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.

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 ContosoRGforVMaz group delete --name ContosoStorageRG

This command removes the group and all the resources that it contains at once.

See also

  • Learn to use Bash with the Azure CLI
  • How to use the Azure CLI effectively
  • How to query Azure CLI command output
How to use variables in Azure CLI commands (2024)

FAQs

How do I pass parameters in Azure command-line? ›

Use hyphen characters in parameters

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

Can you use variables in command line? ›

As Parts of Commands

First, variables can be used as arguments to commands. Take a look at the following example: $ MESSAGE='Hello, world! ' $ echo $MESSAGE Hello, world!

How do you pass a variable in command line? ›

To pass variable values via the command line, use the PrjVar ( pv ) or PSVar ( psv ) arguments for the project and project suite variables respectively. Variable values you pass via the command line are temporary.

How do you pass variables in Azure pipeline? ›

setvariable , the following tasks can use the variable using macro syntax $(myVar) . The variable will only be available to tasks in the same job by default. If you add the parameter isoutput , the syntax to call your variable changes. See Set an output variable for use in the same job.

How to specify parameters in command line? ›

Any parameter with an argument can be entered as -parameter=argument . Entering -param= means the argument is an empty string, equivalent to -param . The symbol '--' is valid as the next to last entry on the command line. It specifies that the next entry is the target object name or ID, even if it begins with a hyphen.

How do I run commands in Azure? ›

Go to a VM in the Azure portal and select Run command from the left menu, under Operations. You see a list of the available commands to run on the VM. Choose a command to run. Some of the commands might have optional or required input parameters.

How does CLI work in Azure? ›

The Azure Command-Line Interface (CLI) is a cross-platform command-line tool to connect to Azure and execute administrative commands on Azure resources. It allows the execution of commands through a terminal using interactive command-line prompts or a script.

How to show environment variables in command line? ›

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.

How do you define a variable in PowerShell? ›

In PowerShell, variables are represented by text strings that begin with a dollar sign ( $ ), such as $a , $process , or $my_var . Variable names aren't case-sensitive, and can include spaces and special characters.

How to define a variable in CMake command line? ›

Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.

How do you define a variable in ADF? ›

Select the "Variables" tab, and click on the "+ New" button to define a new variable. Enter a name and description for the variable, and select its data type from the dropdown menu. Data types can be String, Bool, or Array. Optionally, you can also assign a default value to the variable.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5596

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.