2

When I run the following program

for i in {0..18}; do 
        for j in {0..18}; do
                for k in {0..18}; do
                        echo $i $j $k   
                done
        done
done

I get the disired output like

0 0 0
0 0 1
0 0 2

and so on

However when I calculate the loop size like this:

steps=$((360/20))


echo $steps
for i in {0..$steps}; do 
        for j in {0..$steps}; do
                for k in {0..$steps}; do
                        echo $i $j $k   
                done
        done
done

I get the following output:

{0..18} {0..18} {0..18}

How can I get the desired output like in the first script? What's my error here?

Kev1n91
  • 3,553
  • 8
  • 46
  • 96
  • 1
    Simple fact.. range expansion happens before parameter expansion, the loop will never work! – Inian Aug 11 '17 at 12:31
  • Thank you for your answer, so basically, the loop itself does not expand over the parameters, cause the range gets expanded before the parameter gets expanded ? – Kev1n91 Aug 11 '17 at 12:35
  • 1
    The title may not seem like an exact duplicate, but excluding the answers involving `eval`, ones with for loop with `for ((..))` is the right way to do! – Inian Aug 11 '17 at 12:36
  • 1
    Yes you got it right! – Inian Aug 11 '17 at 12:36

0 Answers0