key_1 = "foo"
key_2 = "boo"
val_1="foo 1"
val_2="boo 2"
tags=$key_1=$val_1 $key_2=$val_2
--tags $tags <- this command will give me error as it will parse the space in val_1 and val_2
please help. Thanks
_Originally posted by @andythsu in https://github.com/Azure/azure-cli/issue_comments#issuecomment-432701964_
@andythsu the short answer to your question is, there is no solution due to limitations of the bash shell.
export key1="key"
export val1="val 1"
export tags="$key1=$val1"
echo $tags
> key=val 1
The CLI will not accept the above.
export key1="key"
export val1="val 1"
export tags="$key1=\"$val1\""
echo $tags
> key="val 1"
This looks like is should work, but when you run:
az foo create --tags $tags
You end up with:
"tags": {
"1\"": "",
"foo": "\"foo"
}
is there a wordaround to this? such as can I create with no tags first, and then update? or it's simply not possible
You'd get the same result with update. The issue is that the bash shell either swallows the quotes entirely (1st case) or forces their preservation with (second).
yes... so I can only do --tags key="value", can't dynamically add keys right?
Well, you can do --tags key="value 1" as well, but not through $ expansion (at least not on bash). We will discuss whether we can implement a feasible workaround.
The workaround we've found out is to create tags with no spaces, then update each tag
running az group update --name $name --set tags."$key=$value" somehow works
@andythsu you can do:
key=key
val="val 1"
az foo --tags "$key"="$val"
or
key=key
val="val 1"
tag="$key=$val"
az foo --tags "$tag"
or
tag1="key=val 1"
tag2="key2=val 2"
az foo --tags "$tag1" "$tag2"
Please let me know if you have other scenarios you were trying to make work.
This works for the single tag case, but not for his original question:
```bash
export key1=key1
export val1="val 1"
export key2=key2
export val2="val 2"
export tags=$key1=$val1 $key2=$val2
az foo --tags "$tags"
"tags": {
"key1": "val 1 key2=val 2"
}
Use arrays in that case:
tags=( "$key=$val1" "$tag1" )
az foo --tags --tags "${tags[@]}"
@andythsu if you would rather not use arrays, please use the examples I mentioned before, which should already let you bypass the restrictions of having to subsequently update and avoid spaces.
Thanks @williexu. I'm closing this issue.
@williexu Thank you for the suggestions!
@tjprescott Thanks for the comments too!