Translate

Image of Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
Image of XSLT 2.0 and XPath 2.0 Programmer's Reference (Programmer to Programmer)
Image of Beginning Google Maps API 3
Image of Modern Operating Systems (3rd Edition)

Zero Padding Brace Expansions in the Korn Shell

Both bash and zsh shells support leading zeros in ranges:

$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
$ echo {01..10}
01 02 03 04 05 06 07 08 09 10
$ echo {001..010}
001 002 003 004 005 006 007 008 009 010
$

From the bash manpage 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

If you want a comma separated range of numbers, you can do something like:

$ echo {01..10} | tr ' ' ','
01,02,03,04,05,06,07,08,09,10

The Korn shell does not support the leading zero syntax in brace expansion. However it does support an equally powerful alternative syntax using %0d:

$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
$ echo {01..10%02d}
01 02 03 04 05 06 07 08 09 10
$ echo {01..10%03d}
001 002 003 004 005 006 007 008 009 010

$ echo {01..10%02d} | tr ' ' ','
01,02,03,04,05,06,07,08,09,10

Finally, you can also use this style of syntax to create a range of leading zero hexadecimals:

$ echo {7..18%02x}
07 08 09 0a 0b 0c 0d 0e 0f 10 11 12

Comments are closed.