Monday, May 30, 2011

Zero Padding in Bash

I was wondering what is the best way to do "Zero Padding" in Bash. Suppose you need to iterate through a for loop and you need 01, 02, 03 sequence instead of 1, 2,3 then that is Zero padding.

Some of the methods I could get from the internet.

1. Using printf

for ((i=2;i<16;i++)); do printf "%02d" $i; done

2. Using seq

There were couple of solutions when you use seq

* seq -f '%02g' 1 15

* seq -w 1 15

Enjoy being fast using bash..