3

This, of course, works:

$ echo {1..5}
1 2 3 4 5

But when i attempt to substitute the number 5 with a variable, this no longer works:

$ f=5; echo {1..$f}
{1..5}

$ f=5; echo {1..${f}}
{1..5}

$ f=5; echo {1..f}
{1..f}

Is there a way to substitute the number within a variable before the {#..#} construct is parsed?

v010dya
  • 543
  • 5
  • 11
  • 4
    Bash do brace expansion before it does variable expansion. So, no, in bash, you cannot do what you want. Use seq instead. – John1024 Oct 29 '16 at 06:53

1 Answers1

5

There is a way:

:~# f=5 ; eval echo {1..$f}
1 2 3 4 5

Alternative:

:~# f=5 ; echo `seq 1 $f`
1 2 3 4 5
gruntboy
  • 416
  • 1
    Please explain why eval is nessecary, just fixing the script doesn't help anyone learn what they are doing wrong. – Zachary Brady Oct 29 '16 at 15:00
  • Who said it is needed? This is one of the options to solve the problem - as you can see the second solution uses the sequence seq. – gruntboy Oct 29 '16 at 17:58