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?

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.

Leave a Reply

Your email address will not be published. Required fields are marked *