0

Using a two-level for loop and seq works fine, and the code

for i in `seq 0 3`; do for j in `seq 0 3`; do echo $i $j; done; done

gives the expected output:

0 0
0 1
1 0
1 1

But if I want a more customised list of numbers:

for i in '-1 4.5'; do for j in '0 -2.2'; do echo $i $j; done; done

I get the output

-1 4.5 0 -2.2

Is there an easy way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jorgen
  • 3,425
  • 4
  • 31
  • 53
  • 3
    Remove quotes: `for i in -1 4.5; do for j in 0 -2.2; do echo $i $j; done; done` – anubhava Sep 25 '15 at 15:52
  • 1
    Or, to appease the overquoters in the world, `for i in '-1' '4.5'; do ...` – William Pursell Sep 25 '15 at 16:10
  • 1
    It might also be educational to consider the effect of putting quotes around the backticks. That is, surround the process substitution with quotes. eg: `for i in "$(seq 0 3)"; do ...` as opposed to `for i in $(seq 0 3); do ...` – William Pursell Sep 25 '15 at 17:04

2 Answers2

1
var a = [1,2,3];
var b = [1,2,3];

for (i in a) {
    for (j in b) {
        alert (i + j);
    }
}

I have also created a jsfiddle: http://jsfiddle.net/f8k5tpqm/1/

STORM
  • 4,005
  • 11
  • 49
  • 98
1

The usage of single ticks is preventing the shell from tokenizing the list supplied to the for loop:

#!/bin/sh

for i in -1 4.5
do  for j in 0 -2.2
    do  # -- keeps the highly portable printf utility from 
        # interpreting '-' as an argument 
        printf -- "$i" "$j\n" 
    done
done