Kayan Kayan - 6 months ago 12
Bash Question

How to call defined values as a loop in shell

I am facing a problem here. I would like to make a loop with the defined values.

list="-50..50"

for i in {$list}
do ...


It is executing only single string as

for i in -50..50
do ...


My desire execution should be

for i in {-50..50}
do ...

Answer

As you can see you cant use variables in bash's brace expansion, use seq i.e sequence program instead like this:

$ low=10
$ high=20
$ for i in {low..high};do #treated as string
> echo $i
> done
{low..high}
$ for i in {$low..$high};do echo $i; done #values substituted but no brace expansion done by bash
{10..20}
$
$ for i in $(seq $low $high);do echo $i; done
10
11
12
13
14
15
16
17
18
19
20
$
Comments