Saving Azure CLI Command Output to a Variable and Using It Later

In this blog post, we’ll walk you through how to save the output of an Azure CLI command to a variable and use it later in both Command Prompt and Bash.

When working with Azure CLI, you might need to save the output of a command to a variable and use it later in other commands. This is a common scenario when automating tasks or scripting.

For macOS and Linux (using Bash)

Run the Azure CLI command and save its output to a variable using the $(command) syntax. Here’s an example of saving the resource ID of a storage account:

resourceId=$(az storage account show --name <your-storage-account-name> --resource-group <your-resource-group-name> --query id --output tsv)

Now, you can use the variable $resourceId in other Azure CLI commands. For example:

az role assignment create --assignee <assignee> --role "<role-name>" --scope $resourceId

In all the examples, make sure to replace <your-storage-account-name>, <your-resource-group-name>, <assignee>, and <role-name> with appropriate values for your use case.

Processing…
Success! You're on the list.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.