Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Bash substitution in a variable declaration

Tags:

bash

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}) ) 
like image 744
tuergeist Avatar asked Jan 02 '12 11:01

tuergeist


1 Answers

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} )
like image 87
choroba Avatar answered Sep 21 '22 05:09

choroba