I have a basic number for loop which increments the variable num by 1 over each iteration...
for (( num=1; num<=5; num++ ))
do
echo $num
done
Which outputs:
1
2
3
4
5
I'm trying to make it produce the output (add leading zero before $num):
01
02
03
04
05
Without doing:
echo 0$num
Use the following syntax:
$ for i in {01..05}; do echo "$i"; done
01
02
03
04
05
Disclaimer: Leading zeros only work in >=bash-4
.
If you want to use printf
, nothing prevents you from putting its result in a variable for further use:
$ foo=$(printf "%02d" 5)
$ echo "${foo}"
05