3
peterc@peterc-ml video $ VAR1="foo maltese mountain"
peterc@peterc-ml video $ VAR2="bar falcon goat"
peterc@peterc-ml video $ for X in 1 2 3
> do
> echo ??????
> done

The desired output is:

 foo bar
 maltese falcon
 mountain goat

Both VAR1 and VAR2 will always have the same number of elements.

3 Answers 3

2

Make an array out of each.

ARR1=( $VAR1 )
ARR2=( $VAR2 )
for (( i = 0; i < ${#ARR1[@]}; i++)); do 
    echo ${ARR1[i]} ${ARR2[i]}
done

e.g.

$ VAR1="a b c"
$ VAR2="1 2 3"
$ ARR1=( $VAR1 )
$ ARR2=( $VAR2 )
$ for (( i = 0; i < ${#ARR1[@]}; i++)); do 
>     echo ${ARR1[i]} ${ARR2[i]}
> done
a 1
b 2
c 3
Sign up to request clarification or add additional context in comments.

Comments

1

You can use cut(1):

echo ${VAR1} | cut -d " " -f ${X}

2 Comments

Drive-by downvote? This example does work, so it's hardly wrong.
Nice to see downvoting of right solution )) Nobody want to think how to get complete solution from this one... +1
0
paste -d ' ' <(tr ' ' '\n' <<< "$VAR1") <(tr ' ' '\n' <<< "$VAR2")

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.