0
  n=5
  for i in {1..$[n]}
  do 
       echo $i
  done

it gives:

{1..5}

But I think it should output:

1
2
3
4
5

Why it gives such a strange output?

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • 3
    http://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash – Aif Oct 24 '13 at 07:18
  • you cannot use brace-expansion with a parameter in bash due to the order of evaluation. Use `seq` instead. And your syntax is wrong, doesn't matter since it wont work anyway, but still. – Fredrik Pihl Oct 24 '13 at 07:25

1 Answers1

2

That is almost a riddle. The expansion of the braces is being done prior to the variable expansion. The bash beginners guide has some good detail on expansion There are a brazillion ways to do this in bash. You could start with:

n=5
for i in $(eval echo {1..$n})
do
   echo $i
done
thekbb
  • 7,668
  • 1
  • 36
  • 61