Padding zeros using PRINTF command

By xngo on February 24, 2019

# Description:
#   -Example below shows how to pad zeroes to a number using PRINTF command.
#   -It will pad zeroes to the number until the total length is 5.
SHORT_NUBMER=13
PADDING_RESULT=$(printf %05d ${SHORT_NUBMER})
echo ${PADDING_RESULT}
 
LONG_NUBMER=343
PADDING_RESULT=$(printf %05d ${LONG_NUBMER})
echo ${PADDING_RESULT}
 
# OUTPUT:
#  00013
#  00343

Reference

  • http://wiki.bash-hackers.org/commands/builtin/printf

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.