You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs-ref-conceptual/Latest-version/azure-cli-variables.md
+65-23Lines changed: 65 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,7 @@
1
1
---
2
2
title: How to use variables in Azure CLI commands | Microsoft Docs
3
-
description: Learn about specifying values directly in Azure CLI commands by using shell variables, setting a subscription, creating default values, or using persistent values.
3
+
description: Learn about specifying values directly in Azure CLI commands using shell variables, setting a subscription, creating default values, or using persistent values.
4
4
ms.service: azure-cli
5
-
ms.date: 09/19/2024
6
5
ms.custom: template-how-to, devx-track-azurecli
7
6
keywords: azure cli variables, azure cli commands, value of variable, shell variables
8
7
---
@@ -11,17 +10,20 @@ keywords: azure cli variables, azure cli commands, value of variable, shell vari
11
10
12
11
In addition to specifying values directly in a command, you can provide values in several ways:
13
12
14
-
* Use shell variables
15
-
* Set a subscription for use in multiple commands
16
-
* Create default values for some parameters
13
+
- Use shell variables
14
+
- Set a subscription for use in multiple commands
15
+
- Create default values for some parameters
17
16
18
17
This article discusses various ways to specify values in Azure CLI commands.
Azure CLI runs in a shell. This article uses Bash. For information about other scripting languages, see [Choose the right Azure command-line tool](./choose-the-right-azure-command-line-tool.md). You can use variables in Bash to pass values for parameters to commands. Using variables with the Azure CLI also allows reuse of commands, either piecemeal or in scripts.
23
+
Azure CLI runs in a shell. This article uses Bash. For information about other scripting languages,
24
+
see [Choose the right Azure command-line tool][01]. You can use variables in Bash to pass values for
25
+
parameters to commands. Using variables with the Azure CLI also allows reuse of commands, either
26
+
piecemeal or in scripts.
25
27
26
28
This example creates a new storage disk of the same type as the storage disk on an existing virtual machine.
27
29
@@ -62,55 +64,78 @@ az disk create --resource-group $MyResourceGroup --name DestinationDisk --size-g
62
64
```
63
65
64
66
---
65
-
This example shows how to assign values to variables that are reused, like **MyResourceGroup** and **osType**. The command [az vm get-instance-view](/cli/azure/vm#az_vm_get_instance_view) combined with the query `storageProfile.osDisk.osType` returns the disk's OS type. Wrapping the command with `$()` assigns the command's return value to `osType`. To learn more about `--query` and JMESPath queries see [How to query Azure CLI command output using a JMESPath query](./use-azure-cli-successfully-query.md).
66
67
67
-
When you assign a value to a variable from another command, be sure that the command uses a compatible output format. The [az vm get-instance-view](/cli/azure/vm#az_vm_get_instance_view) command uses the `tsv` output format. This option returns values without extra formatting, keys, or other symbols. Some output formats include structure or characters like quotation marks. For more information, see [Output formats for Azure CLI commands](./format-output-azure-cli.md).
68
+
This example shows how to assign values to variables that are reused, like **MyResourceGroup** and
69
+
**osType**. The command [az vm get-instance-view][11] combined with the query
70
+
`storageProfile.osDisk.osType` returns the disk's OS type. Wrapping the command with `$()` assigns
71
+
the command's return value to `osType`. To learn more about `--query` and JMESPath queries see
72
+
[How to query Azure CLI command output using a JMESPath query][05].
68
73
69
-
In this example, the **MySubscription** variable must be in quotation marks. The value of the variable contains spaces, which the command can't parse. If you work only with subscription IDs, you don't need to use quotation marks.
74
+
When you assign a value to a variable from another command, be sure that the command uses a
75
+
compatible output format. The [az vm get-instance-view][11] command uses the `tsv` output format.
76
+
This option returns values without extra formatting, keys, or other symbols. Some output formats
77
+
include structure or characters like quotation marks. For more information, see
78
+
[Output formats for Azure CLI commands][02].
79
+
80
+
In this example, the **MySubscription** variable must be in quotation marks. The value of the
81
+
variable contains spaces, which the command can't parse. If you work only with subscription IDs, you
82
+
don't need to use quotation marks.
70
83
71
84
## Set a subscription
72
85
73
-
Many commands require a specific subscription. Azure resources exist in resource groups, which exist in subscriptions. Azure CLI uses a default subscription when you are in a session. To see your current subscription value, run the [az account show](/cli/azure/account#az_account_show) command:
86
+
Many commands require a specific subscription. Azure resources exist in resource groups, which exist
87
+
in subscriptions. Azure CLI uses a default subscription when you are in a session. To see your
88
+
current subscription value, run the [az account show][08] command:
74
89
75
90
```azurecli
76
91
az account show --output table
77
92
```
78
93
79
-
You might have access to only one subscription. For more information, see [Use Azure subscriptions with Azure CLI](./manage-azure-subscriptions-azure-cli.md)
80
-
You can use the [az account set](/cli/azure/account#az_account_set) command to set your current subscription:
94
+
You might have access to only one subscription. For more information, see
95
+
[Use Azure subscriptions with Azure CLI][03] You can use the [az account set][07] command to set
96
+
your current subscription:
81
97
82
98
```azurecli
83
99
az account set --subscription "My Demos"
84
100
```
85
101
86
-
After you set your subscription, you can omit `--Subscription` parameter. For more information, see [Use Azure subscriptions with Azure CLI](manage-azure-subscriptions-azure-cli.md).
102
+
After you set your subscription, you can omit `--Subscription` parameter. For more information, see
103
+
[Use Azure subscriptions with Azure CLI][14].
87
104
88
105
## Create default values
89
106
90
-
You can set values for some parameters by using the [az config set](/cli/azure/config#az_config_set) command. This example sets a default resource group:
107
+
You can set values for some parameters by using the [az config set][09] command. This example sets a
108
+
default resource group:
91
109
92
110
```azurecli
93
111
az config set defaults.group=ContosoRGforVM
94
112
```
95
113
96
-
After running this command, you can run the following command to create a storage account in the ContosoRGforVM resource group:
114
+
After running this command, you can run the following command to create a storage account in the
115
+
ContosoRGforVM resource group:
97
116
98
117
```azurecli
99
118
az storage account create --name storage135 --location eastus --sku Standard_LRS
100
119
```
101
120
102
-
Notice that there's no resource group specified in the command. For more information, see [Set a default resource group](manage-azure-groups-azure-cli.md#set-a-default-resource-group).
121
+
Notice that there's no resource group specified in the command. For more information, see
122
+
[Set a default resource group][13].
103
123
104
124
> [!TIP]
105
-
> Commands getting values for parameters in different ways can be confusing. If a command gives an unexpected result, such as not being able to find a resource group, there may be a default value.
125
+
> Commands getting values for parameters in different ways can be confusing. If a command gives an
126
+
> unexpected result, such as not being able to find a resource group, there might be a default
127
+
> value.
106
128
>
107
-
> If you encounter an error, run the command again with the parameter and value specified. An explicit value for a parameter always takes precedence over other options.
129
+
> If you encounter an error, run the command again with the parameter and value specified. An
130
+
> explicit value for a parameter always takes precedence over other options.
108
131
109
-
You can specify values for several parameters this way. For more information, see [Azure CLI configuration](azure-cli-configuration.md).
132
+
You can specify values for several parameters this way. For more information, see
133
+
[Azure CLI configuration][12].
110
134
111
135
## Clean up resources
112
136
113
-
If you created resources to try any of the commands in this article, you can remove them by using the [az group delete](/cli/azure/group#az_group_delete) command:
137
+
If you created resources to try any of the commands in this article, you can remove them by using
138
+
the [az group delete][10] command:
114
139
115
140
```azurecli
116
141
az group delete --name ContosoRGforVM
@@ -121,6 +146,23 @@ This command removes the group and all the resources that it contains at once.
121
146
122
147
## See also
123
148
124
-
*[Learn to use Bash with the Azure CLI](./use-azure-cli-successfully-bash.md)
125
-
*[Tips to use the Azure CLI successfully](./use-azure-cli-successfully-tips.md)
126
-
*[How to query Azure CLI command output](./use-azure-cli-successfully-query.md)
0 commit comments