0

Maybe I'm over-thinking this but here's my desired output:

one four seven 
one five eight
one six nine
two four seven
two five eight
two six nine
three four seven
three five eight
three six nine

Here's what I started. I got to the second for loop and totally lost my mind trying to find the solution.

#!/bin/bash

declare -a aaa=("four" "five" "six")
declare -a bbb=("one" "two" "three")
declare -a ccc=("seven" "eight" "nine")


for bs in ${bbb[@]}; do
  for as in ${aaa[@]}, cs in ${ccc[@]}; do
    echo "$bs" "$as" "$cs"
  done
done

1 Answer 1

1

You can't have more than one in in a for clause.

If you need to iterate two arrays simultaneously, iterate over their indices:

#! /bin/bash

declare -a aaa=("four" "five" "six")
declare -a bbb=("one" "two" "three")
declare -a ccc=("seven" "eight" "nine")

for b in "${bbb[@]}" ; do
    for i in "${!aaa[@]}" ; do  # or ccc
        echo "$b" "${aaa[i]}" "${ccc[i]}"
    done
done
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I've updated my original question to better address my problem. I over-simplified the issue originally. Thanks for your help though. I would be grateful if you tried to solve this again.
@zewiblzd: Updated. Next time, please ask a new question if it's significantly different to the original one.

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.