tuergeist
tuergeist

Reputation: 9391

How to use Bash substitution in a variable declaration

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}) ) 

Upvotes: 9

Views: 1824

Answers (1)

choroba
choroba

Reputation: 241828

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} )

Upvotes: 6

Related Questions