# How assign numbers range into a simple bash variable

How assign numbers sequence into bash variable as this fail:

``````\$ n={0..9}; echo \$n
{0..9}
\$ n=\"{0..9}\"; echo \$n
"{0..9}"

\$ eval n={0..9}; echo \$n
\$ eval n=\"{0..9}\"; echo \$n
9

\$ n=`eval {0..9}`; echo \$n
``````

Please guide to the correct one

• What do you want `n` to contain? Perhaps `n = "0 1 2 3 4 5 6 7 8 9"`, i.e. a string of numbers? Or an array of numbers? What do you want to do with this `n`? – berndbausch Apr 19 at 5:25

Assign with:

``````\$ n=({0..9})
``````

Using `n=(x)` means `n` is an array with `x` as its contents.

Then output a full array with:

``````\$ echo "\${n[@]}"
0 1 2 3 4 5 6 7 8 9
``````

or output one element at a time with

``````\$ for i in "\${n[@]}"; do
>   printf "%s " "\$i"
> done
0 1 2 3 4 5 6 7 8 9
``````

or access a specific element with:

``````\$ echo "\${n[0]}"
0
``````

There is lots of info already available if you search for "bash array"

Here's two methods:

1. use an array

``````\$ n=( {0..9} )
\$ declare -p n
declare -a n=([0]="0" [1]="1" [2]="2" [3]="3" [4]="4" [5]="5" [6]="6" [7]="7" [8]="8" [9]="9")
``````
2. expand the range into a single string

``````\$ n=\$(printf "%s" {0..9})
\$ declare -p n
declare -- n="0123456789"
``````