问题
I am trying to pass a list of values from a aws cli query to another command. Even though I have seen plenty of examples in AWS, when I try all the values come together:
policy_versions=`aws iam list-policy-versions --query "Versions[].VersionId" --policy-arn $POLICY_ARN --output text`
echo "policy_versions=$policy_versions"
for ver in $policy_versions; do
echo "first version: $ver"
done
Which then prints out:
policy_versions=v3 v2 v1
first version: v3 v2 v1
My value of ver
is the entire string, it should be v3
then v2
and v1
. But It is instead:
v3 v2 v1
.
I cannot figure out what is wrong.
回答1:
I was able to reproduce your results and I traced the results to a difference between shell and zsh
(which is now used as the default shell in MacOS).
Put simply, you can run this command to make it work the way you expect: setopt shwordsplit
For more details, see: iteration - Shell script - iterate over space separated words/characters (in zsh) - Stack Overflow
来源:https://stackoverflow.com/questions/62593695/how-to-i-loop-through-aws-cli-output