49

Here is myscript.sh

#!/bin/bash
for i in {1..$1};
do
    echo $1 $i;
done

If I run myscript.sh 3 the output is

3 {1..3}

instead of

3 1
3 2
3 3

Clearly $3 contains the right value, so why doesn't for i in {1..$1} behave the same as if I had written for i in {1..3} directly?

codeforester
  • 39,467
  • 16
  • 112
  • 140
spraff
  • 32,570
  • 22
  • 121
  • 229

5 Answers5

70

You should use a C-style for loop to accomplish this:

for ((i=1; i<=$1; i++)); do
   echo $i
done

This avoids external commands and nasty eval statements.

jordanm
  • 33,009
  • 7
  • 61
  • 76
27

Because brace expansion occurs before expansion of variables. http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion.

If you want to use braces, you could so something grim like this:

for i in `eval echo {1..$1}`;
do
    echo $1 $i;
done

Summary: Bash is vile.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
16

You can use seq command:

for i in `seq 1 $1`

Or you can use the C-style for...loop:

for((i=1;i<=$1;i++))
kev
  • 155,172
  • 47
  • 273
  • 272
2

Here is a way to expand variables inside braces without eval:

end=3
declare -a 'range=({'"1..$end"'})'

We now have a nice array of numbers:

for i in ${range[@]};do echo $i;done
1
2
3
Jonathan Cross
  • 675
  • 8
  • 17
1

I know you've mentioned bash in the heading, but I would add that 'for i in {$1..$2}' works as intended in zsh. If your system has zsh installed, you can just change your shebang to zsh.

Using zsh with the example 'for i in {$1..$2}' also has the added benefit that $1 can be less than $2 and it still works, something that would require quite a bit of messing about if you wanted that kind of flexibility with a C-style for loop.

elcaro
  • 2,227
  • 13
  • 15