0

Using kernel 2.6.x

GNU bash, version 4.3.42(1)-release (arm-openwrt-linux-gnu)

In a bash script, how would you write a for loop that produces the following result with the variables below ?

command option option abc option 10
command option option def option 20

VAR1="abc def"

VAR2="10 20"

I can iterate $VAR1, but I don't know how to iterate $VAR2.

for i in $VAR1; do
command option option "$VAR1" option "$VAR2"
done

Should the command line be split into two strings, iterated separately and then concatenated ?

How about using a count variable with an array ? For example ...

count=1
echo ${VAR1["$count"]}

Could a count variable with a while loop work ?

  • 2
    Are you trying to get all permutations of the combinations of the two variables? – DopeGhoti Aug 16 '17 at 17:43
  • Does this answer on SO do what you want? – Paul H. Aug 16 '17 at 17:51
  • Updated the result example. – uihdff Aug 16 '17 at 17:55
  • @DopeGhoti - The relationship between the variables would be 1:1. For example, the first values of each variable would be used - abc and 10. – uihdff Aug 16 '17 at 18:03
3

How about this:

arr1=(abc def)
arr2=(10 20)

And then:

for (( i=0; i<${#arr1[@]}; i++ )); do echo "${arr1[i]} ${arr2[i]}"; done
abc 10
def 20

In your case, the echo statement will then look like:

for (( i=0; i<${#arr1[@]}; i++ )); do 
    echo "command option option ${arr1[i]} option ${arr2[i]}"
done
command option option abc option 10
command option option def option 20

Look here for examples on a C style for loop in bash.

  • I'm getting an error on the first variable line - syntax error: unexpected "(". Quoting the variable values gets rid of that error, but creates another error - syntax error: bad for loop variable. – uihdff Aug 16 '17 at 18:54
  • Strange. Works fine on Debian Stretch with: GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) – maulinglawns Aug 16 '17 at 19:06
  • 2
    @uihdff, that's an error message typically reported by busybox sh (based on ash, that doesn't have array support), not bash. You're not having this code interpreted by bash. – Stéphane Chazelas Aug 16 '17 at 19:10
  • #!/bin/sh is at the top of the script and the commands followed on the next lines. Running "bash --version" reports "GNU bash, version 4.3.42(1)-release (arm-openwrt-linux-gnu)". Why is ash being used ? – uihdff Aug 16 '17 at 20:26
  • Had to change the shebang to "#!/opt/bin/bash". It's working now. Tack så mycket ! – uihdff Aug 16 '17 at 20:59
2

It's easier with zsh:

$ a=(a b c) b=(1 2 3)
$ for i j (${a:^b}) echo "i: $i, j: $j"
i: a, j: 1
i: b, j: 2
i: c, j: 3

${a:^b} is the array-zipping operator (expands to a, 1, b, 2, c, 3 above).

If you wanted to loop over words resulting of the split+glob operator applied to $VAR1 and $VAR2 in a POSIX-like shell (like the busybox sh you seem to be using according to the comments to @maulinglawns's answer), you could use the positional parameters.

VAR1='a b c'
VAR2='1 2 3'
set -- $VAR1
for j in $VAR2; do
  echo "i: $1, j: $j"
  shift
done
  • Did the for line need ";do" at the end ? Thanks for the help. – uihdff Aug 16 '17 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.