thomassantosh
thomassantosh

Reputation: 607

Azure CLI echo command not responding

I'm looking to store my connection string in the following variable:

connectionString= az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv

When I execute the above, I get the full connection string in the response. However, when I input:

echo $connectionString

...I get a blank response. The variable is not being stored. Any recommendations on what else to try?

Upvotes: 0

Views: 3363

Answers (2)

holger
holger

Reputation: 975

You could use command substitution in order to capture the output in a variable:

connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)

If you need to preserve output across multiple lines, i.E. when the Azure CLI returns values in JSON format, you may want to use a slightly different format for the output to stdout.

Consider this example:

varResourceGroup=$(az group show -n $resourceGroup)

Using the same command as in your example for the output to stdout would result in a single line:

echo $varResourceGroup 
{ "id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>", "location": "westeurope", "managedBy": null, "name": "<resourceGroup_name>", "properties": { "provisioningState": "Succeeded" }, "tags": null }

If you use a sightly different format, the line breaks are preserved:

echo "$varResourceGroup"
{
  "id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>",
  "location": "westeurope",
  "managedBy": null,
  "name": "<resourceGroup_name>",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null
}

Upvotes: 1

Jason Ye
Jason Ye

Reputation: 13974

As Holger said, we can use this script to define the variable:

connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)

Also, we can use this way to define this variable, like this:

[root@jasoncli@jasonye ~]# connectionstring=`az storage account show-connection-string -n jasondisk3 -g jasonauto --query connectionString -o tsv`
[root@jasoncli@jasonye ~]# echo $connectionstring
DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=jasondisk3;AccountKey=m+kQwLuQZiI3LMoMTyAI8KxxxxD+ZaT9HUL3Agxxxxqul4s8fAIHGPMTD/AG2j+TPHBpttq5hXRmTaQ==

Hope this helps.

Upvotes: 1

Related Questions