Variables in bash seq replacement ({1..10}) [duplicate]
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})
Solution 1:
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
Solution 2:
You should consider using seq(1)
. You can also use eval:
eval echo {$start..$end}
And here is seq
seq -s' ' $start $end
Solution 3:
You have to use eval
:
eval echo {$start..$end}
Solution 4:
If you don't have seq
, you might want to stick with a plain for loop
for (( i=start; i<=end; i++ )); do printf "%d " $i; done; echo ""