11

I am trying to use variable in azure CLI like we used in powershell.

In powershell we define variable as follows

$LOCATION = value

And used it in command as follows

az group create --name foo --location $LOCATION

What I have tried :-

I have tried to find it out in Microsoft documentation

https://learn.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest

but I did not get any information about that.

Question :-

  1. How we can define variable in azure CLI?(like powershell)
  2. How we can used it in command?(like powershell)

Note:- I have installed azure CLI at my local.

Harish
  • 789
  • 1
  • 7
  • 21

4 Answers4

8

The easiest way to pass variables to any CLI command is by using environment variables

An environment variable is a variable whose value is set outside the program, typically through a functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.

Below you can find examples in Bash and CMD:

Bash-

Set new environment variable-

export LOCATION=westeurope

Print the environment variable-

echo ${LOCATION}

AZ CLI example-

az group create --name foo --location ${LOCATION}

CMD-

Set new environment variable-

set LOCATION=westeurope

Print the environment variable-

echo %LOCATION%

AZ CLI example-

az group create --name foo --location %LOCATION%
Amit Baranes
  • 7,398
  • 2
  • 31
  • 53
1

It is the same way you do it in powershell,

To assign a value

sajeetharan@Azure:~$ LOCATION="eastus"

To check value is set,

sajeetharan@Azure:~$ echo $LOCATION
eastus
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You could do it like this:

New-Variable -Name "location" -Visibility Public -Value "eastus"
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
0

Azure Cloud Shell

Assignment: use double quotes if you are assigning a long string (export is not needed):

AZURE_STORAGE_CONNECTION_STRING="DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=MYACCNAME;AccountKey=MYACCKEY"

Usage: invoke/surround it with ${}, example:

  • checking Storage Queue messages:

    az storage message peek \
      --connection-string ${AZURE_STORAGE_CONNECTION_STRING} \
      --queue-name MYQUEUE
    
  • printing:

    echo ${AZURE_STORAGE_CONNECTION_STRING}
    
CPHPython
  • 12,379
  • 5
  • 59
  • 71