9

I try to declare a variable x with all chars from a..x. On the command line (bash), substitution of a..x works w/o any ticks.

$ echo {a..x}
a b c d e f g h i j k l m n o p q r s t u v w x

But assigning it to variable via x={a..x} results in {a..x} as string. Only x=$(echo {a..x}) works.

The question is: Is this the proper way of assignment or do I have to do other things?

The main aim is to assign the sequence to an array, e.g.,

disks=( $(echo {a..x}) ) 
tuergeist
  • 9,171
  • 3
  • 37
  • 58
  • 1
    brace expansion fails on assignment because it produces a list and the variable only accepts a scalar value; bash is "smart" in this sense because it knows it can't assign a list to the variable so it doesn't expand it all, that's the same reason you can assign variables containing spaces without quoting them, since it's a scalar value bash doesn't apply word-splitting on the contents even when unquoted. the array however do accepts a list and in that case both brace expansion and word-splitting happens. – Samus_ Jan 02 '12 at 21:33

1 Answers1

6

You can also use set (but be sure to save positional parameters if you still need them):

set {a..x}
x="$@"

For arrays, brace expansion works directly:

disks=( {a..x} )
choroba
  • 231,213
  • 25
  • 204
  • 289