In this blog post, we’ll explore how to use variables with Azure CLI, step-by-step. We’ll cover how to declare variables, and how to use them in Azure CLI commands
When working with Azure CLI, you may encounter scenarios where you need to use variables to store and reuse values such as resource group names, storage account names, or subscription IDs.
Declare Variable
To get started, we start by declaring a variable using the following command.
Declare a variable using the following syntax.
VAR_NAME=value
For Example
RESOURCE_GROUP=myresourcegroup
Use the variable in Azure CLI commands by enclosing the variable name in curly braces, preceded by a dollar sign. For example, to create a virtual machine with the resource group name stored in the RESOURCE_GROUP
variable, you can use the following command:
az vm create --resource-group ${RESOURCE_GROUP} --name myVM --image UbuntuLTS
You can also use variables in combination with other shell commands, such as if
statements and loops. For example:
if [[ $RESOURCE_GROUP == "myresourcegroup" ]]; then
az group delete --name ${RESOURCE_GROUP} --yes
fi
Note that variables are case-sensitive, so make sure to use the same capitalization throughout your commands. Also, be careful not to include spaces around the equals sign when declaring a variable
Visit Jooble Company to find the best opportunities for developers