How to create a sequence with leading zeroes using brace expansion
Prefix the first number with a 0
to force each term to have the same width.
$ echo {08..10}
08 09 10
From the bash man page section on Brace Expansion:
Supplied integers may be prefixed with 0 to force each term to have the same width. When either x or y begins with a zero, the shell attempts to force all generated terms to contain the same number of digits, zero-padding where necessary.
Also note that you can use seq
with the -w
option to equalize width by padding with leading zeroes:
$ seq -w 8 10
08
09
10
$ seq -s " " -w 8 10
08 09 10
If you want more control, you can even specify a printf style format:
$ seq -s " " -f %02g 8 10
08 09 10
if you use printf
printf "%.2d " {8..10}
this will force to be 2 chars and will add a leading 0. In case you need 3 digits you can change to "%.3d ".
I have the same bash version of the original poster (GNU bash, version 3.2.51(1)-release
) and {08..12}
doesn't work for me, but the following does:
for i in 0{8..9} {10..12}; do echo $i; done
08
09
10
11
12
It's a little more tedious, but it does work on the OP version of bash (and later versions, I would assume).