7
votes

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://docs.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.

4

4 Answers

6
votes

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%
1
votes

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
0
votes

You could do it like this:

New-Variable -Name "location" -Visibility Public -Value "eastus"
0
votes

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}