Yusuf
Yusuf

Reputation: 13

Using bash to get Two variables in for-loop form two different lists

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

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

Upvotes: 0

Views: 4710

Answers (2)

chepner
chepner

Reputation: 531215

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

Upvotes: 1

Armali
Armali

Reputation: 19375

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

Upvotes: 1

Related Questions