2

This question already has an answer here:

I am trying to enumerate a range of integers using a variable but having some trouble. When I type

echo {1..5}

I get

1 2 3 4 5

However, when I type a variable, I am unable to get the enumeration. For example

$ num=5
$ echo {1..$num}
{1..5}

I am stumped as to why the result is not the same. I have tried using quotes and that has not helped either. Can someone help or explain?

marked as duplicate by Gilles bash Apr 24 '15 at 5:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Brace expansion happens before variable expansion in bash... – jasonwryan Apr 23 '15 at 3:24
  • So, is there a workaround for that? – unxnut Apr 23 '15 at 3:25
  • You could use eval, but there are probably cleaner solutions. – jasonwryan Apr 23 '15 at 3:27
  • 1
    One other workaround is to use ksh. – Janis Apr 23 '15 at 3:57
  • The eval way: eval echo \{1..$num\} – Peter.O Apr 23 '15 at 7:24
3

You may use this:

 num=5;
 echo $(seq $num)

Gives:

 1 2 3 4 5

Remark: $(...) syntax is command substitution.

  • 5
    Or just seq -s " " $num, thus avoiding echo and subshell expansion. – Janis Apr 23 '15 at 3:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.