How to padding a integer with 0s in bash?

How to convert an integer into a string of many characters padded with 0s in bash.

To do this in C:

sprintf(buffer, "%08d", n);

which convert integer n to a 8-character string padded with leading 0s.

How to padding a integer with 0s in bash?

asked Nov 24, 2013 by anonymous

1 Answer

 
Best answer

In bash, you can also use printf

For the previous example:

str=$(printf "%08d" $n)

where $n is the integer and $str contains the converted string.

answered Nov 24, 2013 by anonymous

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...