1

I am trying to pull info from multiple arrays and then echo it out. Unfortunately, I am having some trouble and I am almost positive it is something wrong with my for loop structure.

Lets assume:

array1= 1, 2, 3
array2= toronto, new york, paris

The code that I currently have:

for element in "${array1[@]}" "${array2[@]}"
do
echo ""$element" "element""
done

The output looks something like this:

1 1
2 2
3 3
toronto toronto
new york new york
paris paris

Now this obviously doesn't work because it outputs the same thing twice in each array. So, I tried to use a for loop within a for loop but then I have multiples.

How would I setup the for loop so my output looks like this:

1 toronto
2 new york
3 paris
3
  • 2
    The definition array2= toronto, new york, paris is not valid at all if you want to define an array. Commented Dec 1, 2014 at 15:18
  • 1
    You can't natively loop over multiple arrays. So you have to manually do it by looping numerically from 0 to "${#array1}" (or "${#array1}" depending on which is longer and what behaviour you want if they don't have the same length). Commented Dec 1, 2014 at 15:22
  • just wanted to put down that the question itself answered what google brought me here for. I wanted to loop over two arrays (sequentially) and didn't know you could just concatenate them like that (for element in "${array1[@]}" "${array2[@]}"), I was about to attempt something silly (printf '%s\n' "${array1[@]}" "${array2[@]}" | while read.... gross). So thanks! Commented Jan 30, 2020 at 4:56

3 Answers 3

4
array1= 1, 2, 3
array2= toronto, new york, paris

should be rewritten as :

array1=( 1 2 3 )
array2=( toronto 'new york' paris )

And finally :

for ((i=0; i<${#array1[@]}; i++)); do echo "${array1[$i]} ${array2[$i]}"; done
Sign up to request clarification or add additional context in comments.

3 Comments

This is a very good way to do it. Simple and using the right tools, +1
Yea this is unreal. Exactly what I was looking for. Very efficient!
Doesn't look good because u have to make sure that the array has similar naming
1

You probably want this:

array1=(1 2 3)
array2=(toronto "new york" paris)
paste <(printf "%s\n" "${array1[@]}") <(printf "%s\n" "${array2[@]}")
1   toronto
2   new york
3   paris

Comments

0
#!/bin/bash
declare -a arr2=("1" "2" "3")
declare -a arr=("element" "element" "element")
x=0
while [ $x -le 2 ]
do
echo " "${arr2[x]}" ${arr[x]}"
 x=$(( $x + 1 ))
done

2 Comments

some additional explanation to your solution would be nice (so other readers can understand it easier). On another matter: you might want to think about changing your user name, people could take it wrong and feel affronted, if you know what I mean.
yes dude you are rigth I m newbies on linux and this webside. I try to learn just ask question myself how . and I know my nick means I don't accept their behavior even rock earth what else without soul did not deserve vulgarity . I hope my sences are clear eng is not my laguage

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.