0

Why does this code not execute the for loop in bash but print what it should loop over? How would execute the for loop like this?

read N R 
for i in {0..$[(N-1)*R]..$R};do
echo $i;done

enter image description here enter image description here

Yes the code looks horrible but it was for a least amount of characters code challenge.

1 Answers1

1

One idea for dynamically generating the desired range:

read N R

for i in $(seq 0 ${R} $(( (N-1)*R )) )
do
    echo $i
done

And reducing to the least number of characters:

read N R;for i in $(seq 0 $R $((N*R-R)));do echo $i;done

Though, generally speaking, I'd probably opt for something like:

$ cat ezrah
read N R

for (( i=0 ; i<= (N-1)*R ; i=i+R ))
do
    echo $i
done

A couple sample runs:

$ ezrah
3 4
0
4
8

$ ezrah
6 3
0
3
6
9
12
15
markp-fuso
  • 28,790
  • 4
  • 16
  • 36