0

I'm working with bash and I have two lists:

  • AZU SJI IOP
  • A1 B1 C1

Using the bash code below:

for f1 in AZU SJI IOP
do
    for f2 in A1 B1 C1
    do
        echo $f1 + $f2
    done
done

I get this result:

$ bash dir.sh
AZU + A1
AZU + B1
AZU + C1
SJI + A1
SJI + B1
SJI + C1
IOP + A1
IOP + B1
IOP + C1

I would like to get the result in this way

AZU A1

SJI B1

IOP C1
3
  • 3
    From How do I ask a good question?: "DO NOT post images of code, data, error messages, etc. - copy or type the text into the question" Commented Jul 13, 2021 at 8:21
  • you want three resulting lines, so don't use two nested for loops or you'll have 3*3=9. Use one for loop iterating over 0 to 2, then use that number to index both lists. Moreover you've seen that + isn't a concatenation operator and will appear in the output, so just avoid it. Commented Jul 13, 2021 at 11:37
  • Does this answer your question? zip columns from separate files together in bash Commented Jul 13, 2021 at 11:38

2 Answers 2

1

Define two arrays such that they have the same indices, then iterate over the indices of one array:

list1=(AZU SJI IOP)
list2=(A1 B1 C1)

for i in "${!list1[@]}"; do
  echo "${list1[i]} ${list2[i]}"
done
Sign up to request clarification or add additional context in comments.

Comments

1

You could of course use paste, but since your lists are not in files, you might be interested in a solution without external commands:

set -- A1 B1 C1
for f1 in AZU SJI IOP
do  echo $f1 $1
    shift
done

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.