Tag Archives: scripting

Padding a Numeric in Bash

I needed to pad a day of the month value to 2 places in a bash script.

This is made easy by the GNU program printf, which is part of standard distributions of Linux. In the following script snippet, the current day of the month is passed from the command invocation (or, if not specified, defaulted to the current day). It is then zero-padded with printf.

TODAY=$(date +%d)
if [[ "$1" != "" ]]; then
TODAY=$(printf "%02d" $TODAY) # Zero pad day.