Nested loop code

Greetings,

Would anyone be able to tell me why this nested loop doesn't seem to work in any variation?

for i in {1..8}
    do
        echo "i is "$i
        for j in {1..i}
            do
                echo "j is "$j
        done
    done

output is always along the lines of

i is 1
j is {1..i}
i is 2
j is {1..i}

I have tried, as replacements:

for j in {1..$i}
for j in {1.."$i"}
for j in {1..'$i'}
for j in {1..`echo $i`}

As you can see, when my Unix code doesn't work I generally just flail around until it does.

Any explanation would be greatly appreciated. Thanks!

Looking at it

for j in {1..$i}

should be good...

You forgot to mention what shell you were using for I believe {1..8} is not very ksh or sh...
Give it a try with bash, I am quite sure it will work...

for i in {1..3}
do
  echo "i is $i"
  for (( j=1; j<=i; j++ ))
  do
    echo "j is "$j
  done
done

Had a look... as Kristinu noticed... the syntax {1..$i} does not work for it wants a value to value and not a variable...

Try this as as alternative to using the {1..8} syntax which not all shells support:

#!/usr/dt/bin/dtksh

for ((i=1;i<9;i++))
do
  echo "i is ${i}"
  for ((j=1;j<$i+1;j++))
  do
    echo "\tj is ${j}"
  done
done

exit 0

Output:

i is 1
        j is 1
i is 2
        j is 1
        j is 2
i is 3
        j is 1
        j is 2
        j is 3
i is 4
        j is 1
        j is 2
        j is 3
        j is 4
etc

demo:

with nestedloop:
i=1
j=1
k=8
for i in {1..$k}
do
  echo "i is "$i
  for j in {1..8}
  do
       echo "j is "$j
       sleep 1
  done
done
~
n12:/home/vbe $ bash nestedloop
i is {1..8}
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
j is 7
j is 8

nestedloop:
i=1
j=1
k=8
for i in {1..8}
do
  echo "i is "$i
  for j in {1..$i}
  do
       echo "j is "$j
       sleep 1
  done
done
n12:/home/vbe $ bash nestedloop
i is 1
j is {1..1}
i is 2
j is {1..2}
i is 3
j is {1..3}
i is 4
j is {1..4}
i is 5
j is {1..5}
i is 6
j is {1..6}
i is 7
j is {1..7}
i is 8
j is {1..8}

and nestedloop:
i=1
j=1
k=8
for i in {1..8}
do
  echo "i is "$i
  for j in {1..6}
  do
       echo "j is "$j
       sleep 1
  done
done
n12:/home/vbe $ bash nestedloop
i is 1
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
i is 2
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
i is 3
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
i is 4
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
i is 5
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
i is 6
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
i is 7
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6
i is 8
j is 1
j is 2
j is 3
j is 4
j is 5
j is 6

forgot to show with ksh:

12:/home/vbe $ ksh nestedloop 
i is {1..8}
j is {1..6}

Thank you kristinu. That reminds me of old Pascal loops! I had no idea you could use that terminology.

I also made it work with a while loop:

while [ $j -le $i ]
            do
                echo "j is "$j
                j=$(( $j + 1 ))
            done

but I wanted to understand how it could be done with a FOR so thank you.

---------- Post updated at 11:06 AM ---------- Previous update was at 11:03 AM ----------

Thank you gary_w for clarifying that I am depending on bash.