How to add leading zeros for for-loop in shell? [duplicate]

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

7 Answers
7

Leave a Comment