Skip to content

Commit 3cbc989

Browse files
authored
Refreshed, reflowed, converted to link refs (#5332)
1 parent f698539 commit 3cbc989

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

docs-ref-conceptual/Latest-version/azure-cli-variables.md

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
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.
44
ms.service: azure-cli
5-
ms.date: 09/19/2024
65
ms.custom: template-how-to, devx-track-azurecli
76
keywords: azure cli variables, azure cli commands, value of variable, shell variables
87
---
@@ -11,17 +10,20 @@ keywords: azure cli variables, azure cli commands, value of variable, shell vari
1110

1211
In addition to specifying values directly in a command, you can provide values in several ways:
1312

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
1716

1817
This article discusses various ways to specify values in Azure CLI commands.
1918

2019
[!INCLUDE [include](~/articles/reusable-content/azure-cli/azure-cli-prepare-your-environment.md)]
2120

2221
## Use shell variables
2322

24-
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.
2527

2628
This example creates a new storage disk of the same type as the storage disk on an existing virtual machine.
2729

@@ -62,55 +64,78 @@ az disk create --resource-group $MyResourceGroup --name DestinationDisk --size-g
6264
```
6365

6466
---
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).
6667

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].
6873

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.
7083

7184
## Set a subscription
7285

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:
7489

7590
```azurecli
7691
az account show --output table
7792
```
7893

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:
8197

8298
```azurecli
8399
az account set --subscription "My Demos"
84100
```
85101

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].
87104

88105
## Create default values
89106

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:
91109

92110
```azurecli
93111
az config set defaults.group=ContosoRGforVM
94112
```
95113

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:
97116

98117
```azurecli
99118
az storage account create --name storage135 --location eastus --sku Standard_LRS
100119
```
101120

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].
103123

104124
> [!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.
106128
>
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.
108131
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].
110134

111135
## Clean up resources
112136

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:
114139

115140
```azurecli
116141
az group delete --name ContosoRGforVM
@@ -121,6 +146,23 @@ This command removes the group and all the resources that it contains at once.
121146

122147
## See also
123148

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)
149+
- [Learn to use Bash with the Azure CLI][04]
150+
- [Tips to use the Azure CLI successfully][06]
151+
- [How to query Azure CLI command output][05]
152+
153+
<!-- link references -->
154+
155+
[01]: ./choose-the-right-azure-command-line-tool.md
156+
[02]: ./format-output-azure-cli.md
157+
[03]: ./manage-azure-subscriptions-azure-cli.md
158+
[04]: ./use-azure-cli-successfully-bash.md
159+
[05]: ./use-azure-cli-successfully-query.md
160+
[06]: ./use-azure-cli-successfully-tips.md
161+
[07]: /cli/azure/account#az_account_set
162+
[08]: /cli/azure/account#az_account_show
163+
[09]: /cli/azure/config#az_config_set
164+
[10]: /cli/azure/group#az_group_delete
165+
[11]: /cli/azure/vm#az_vm_get_instance_view
166+
[12]: azure-cli-configuration.md
167+
[13]: manage-azure-groups-azure-cli.md#set-a-default-resource-group
168+
[14]: manage-azure-subscriptions-azure-cli.md

0 commit comments

Comments
 (0)