Variables in bash seq replacement ({1..10})

Tyilo picture Tyilo · May 31, 2011 · Viewed 94.1k times · Source

Is it possible to do something like this:

start=1
end=10
echo {$start..$end}
# Ouput: {1..10}
# Expected: 1 2 3 ... 10 (echo {1..10})

Answer

anubhava picture anubhava · May 31, 2011

In bash, brace expansion happens before variable expansion, so this is not directly possible.

Instead, use an arithmetic for loop:

start=1
end=10
for ((i=start; i<=end; i++))
do
   echo "i: $i"
done

OUTPUT

i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
i: 10