Adding Leading Zero in Bash

Wander Boessenkool wander at kanslozebagger.org
Sun Oct 22 11:23:46 UTC 2006


On 10/22/06, OOzy Pal <oozypal at gmail.com> wrote:
> Ok, but I don't know number before hand, I mean that I might come a
> cross 15. Then what would happend am I gona get 015.
>
> I would like to add zero when the number is between 0-9 and ignors the
> zero for number >9

You could use printf formatting: printf %02d $VAR

Or if it needs to go into a anoter variable:
OTHERVAR=$(printf %02d $VAR)

See "man 1 printf" for more info

-- 
Wander




More information about the ubuntu-users mailing list