0

I have the following problem in a bash script. I'm trying to use a variable N as the upper bound in a simple range, like

#!/bin/bash

N=10

for n in {1..$N};
do
    echo $n
done;

However, instead of displaying each number in the loop, the code above literally displays {1..10}. If I change $N to its value, i.e. 10, things work as expected. How can I overcome this and use the variable as the upper limit of the range?

vsoftco
  • 55,410
  • 12
  • 139
  • 252

1 Answers1

1

You can't do this, instead, do that :

n=10
for ((i=0; i<=n; i++)); do
    echo $i
done

or :

n=10
for i in $(seq 0 $n); do
    echo $i
done