I am trying to run an Azure CLI command inside a shell script and unable to resolve the below issue.
Operation I am trying to do : Delete all resource groups starting with 'test' within a subscription. In this regard when I execute the below commands for deleting the resource group individually outside of the script, it works fine :
az login
rg='test.example1'
az group delete -g $rg --subscription <sub-id> -y
However, when I try to do the below inside the shall script, I am getting a validation error : ValidationError: Parameter 'g' must conform to the following pattern: '^[-\w\._\(\)]+$'.
This is my shell script snippet :
for rg in $(az group list --subscription <sub-id> --query [].name); do
if [[ "$rg" =~ test* ]];then
echo "Deleting resource group : $rg"
az group delete -g $rg --subscription <sub-id> -y
fi
done
The echo is showing the resource group as expected within quotes, like
Deleting resource group :"test.example1"
I have a hunch that the $rg in the az group delete command is not being read correctly. However, I am unable to figure out how I can fix that.
Update : Added the screenshot @CharlesXu. Please note I added the break to limit the deletion to just one resource group during testing.



rg = 'test.example1'does not assign a value torg. I would suspect you get an error message from that line.if [[ "$rg" =~ test* ]]probably isn't doing what you want -- it'll match if the value contains "tes" anywhere. This is because=~does regular expression matching, and the regextest*matches "tes" followed by zero or more "t" characters (and the match isn't anchored, so it'll match anywhere in the string). You probably want a glob pattern match instead; to get that, use=instead of=~.echo "Deleting resource group : $rg"