0
for PRODUCT in ${AAA} ${BBB} ${CCC}; do
  for GITBRANCH in ${AAA_BRANCH} ${BBB_BRANCH} ${CCC_BRANCH}; do
    cd ${PRODUCT}
    git checkout ${GITBRANCH}
done;
done

My nested for loop in bash (above) does:

  • cd under AAA repo and check out AAA_BRANCH
  • cd under AAA repo and check out BBB_BRANCH
  • cd under AAA repo and check out CCC_BRANCH
  • cd under BBB repo and check out AAA_BRANCH
  • cd under BBB repo and check out BBB_BRANCH
  • cd under BBB repo and check out CCC_BRANCH
  • cd under CCC repo and check out AAA_BRANCH
  • cd under CCC repo and check out BBB_BRANCH
  • cd under CCC repo and check out CCC_BRANCH

I want my loop to:

  • cd under AAA repo and check out AAA_BRANCH
  • cd under BBB repo and check out BBB_BRANCH
  • cd under CCC repo and check out CCC_BRANCH

How can I accomplish this? Thx

1 Answer 1

2

Use arrays instead and iterate over indices.

products=(aaa bbb ccc)
branches=(aaa_branch bbb_branch ccc_branch)
for i in "${!products[@]}"; do
  cd "${products[i]}"
  git checkout "${branches[i]}"
  cd -
done
Sign up to request clarification or add additional context in comments.

Comments

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.