1

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 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

!!!!!!!!!!!!!!!!!!!!!!!!THANKYOU!!!!!!!!!!!!!!!!!!!!!!!!!!
Seriously this thing lost me like hours haha Thought I was going crazy much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.