Shell – How to iterate two variables in a sh script

shellshell-script

Using kernel 2.6.x

How would you script the result below with the following variables using sh (not bash, zsh, etc.) ?

VAR1="abc def ghi"
VAR2="1 2 3"
CONFIG="$1"

for i in $VAR1; do
   for j in $VAR2; do
      [ "$i" -eq "$j" ] && continue
   done
command $VAR1 $VAR2
done

Desired result:

command abc 1
command def 2
command ghi 3

Best Answer

One way to do it:

#! /bin/sh

VAR1="abc def ghi"
VAR2="1 2 3"

fun()
{
    set $VAR2
    for i in $VAR1; do
        echo command "$i" "$1"
        shift
    done
}

fun

Output:

command abc 1
command def 2
command ghi 3
Related Question