Brace expansion {...}
is very ahead of Parameter Expansions ${..}
.
One way to re-process the line and get the values inside $var to perform the Brace expansion is to use eval
(Not recommended and not really needed in this case):
lowest=046725
highest=046899
for for seq in $(eval echo {$lowest..$highest});
do
#body
done
It is better if the numbers for the limits do not have leading zeros, but if they do, this will also convert them.
The output will have a leading zero, as requested. And is controlled by the single printf
. Change the 06
to 07
if you need two zeros.
#!/bin/bash
lowest=046725 highest=046899
seq="$(( 10#$lowest ))" max="$(( 10#$highest ))"
while (( seq <= max ))
do printf '%06d\n' $(( seq++ ))
done
If you need the result in a variable, use this line (and use var):
do printf -v var '%06d\n' $(( seq++ ))
And a sh
version:
#!/bin/sh
lowest=00046725 highest=000046899
seq=${lowest#"${lowest%%[!0]*}"}
max=${highest#"${highest%%[!0]*}"}
while [ $seq -le $max ]
do printf '%06d ' "$seq"
seq=$(( seq + 1 ))
done