How to add leading zeros for a for-loop in a shell?

I have a base number for a loop that increments each num variable by 1 by every iteration ...

for (( num=1; num<=5; num++ )) do echo $num done 

What outputs:

 1 2 3 4 5 

I am trying to get it to produce a result (add a leading zero to $ num):

 01 02 03 04 05 

Without execution:

 echo 0$num 
+63
bash for-loop
Aug 27 '13 at 8:03
source share
7 answers

Use the following syntax:

 $ for i in {01..05}; do echo "$i"; done 01 02 03 04 05 

If you want to use printf , nothing prevents you from putting its result in a variable for future use:

 $ foo=$(printf "%02d" 5) $ echo "${foo}" 05 

Disclaimer: this only works in >=bash-4 .

+106
Aug 27 '13 at 8:37
source share

seq -w will determine the width and normalize it.

 for num in $(seq -w 01 05); do ... done 

Apparently, this does not work in the latest versions of iOS, so you can install macports and use its seq version, or you can explicitly set the format:

 seq -f '%02g' 1 3 01 02 03 

But given the ugliness of the format specifications for such a simple problem, I prefer the Henk and Adrian solution that just uses Bash. Apple can't mess it up, as there is no generic version of the unix version of Bash:

 echo {01..05} 

Or:

 for number in {01..05}; do ...; done 
+30
Aug 27 '13 at 8:05
source share

Use the printf command to add 0 :

 printf "%02d\n" $num 

Your loop will look like this:

 for (( num=1; num<=5; num++ )); do printf "%02d\n" $num; done 01 02 03 04 05 
+21
Aug 27 '13 at 8:05
source share

I am not interested in displaying it (which is mainly used by printf, right?) The $ num variable will be used as a parameter for another program, but let me see what I can do with this.

You can use printf :

 for num in {1..5} do value=$(printf "%02d" $num) ... Use $value for your purposes done 
+10
Aug 27 '13 at 15:17
source share

why not printf '%02d' $num ? See help printf for this bash internal command.

+5
Aug 27 '13 at 8:04 on
source share

Starting with bash 4.0, you can use the Brace extension with fixed length strings. See below the original announcement.

It will do what you need and does not require anything external to the shell.

 $ echo {01..05} 01 02 03 04 05 for num in {01..05} do echo $num done 01 02 03 04 05 



From bash / NEWS file

This is a brief description of the new features added in bash -4.0, since the release of bash -3.2.

.,.

d. The bracket extension now allows zero padding of extended numeric values ​​and adds the correct number of zeros to ensure that all values ​​contain the same number of digits.

+3
Aug 27 '13 at 9:05
source share

Just a note: I experienced different types of behavior in different versions of bash:

  • version 3.1.17 (1) -release- (x86_64-suse-linux) and
  • Version 4.1.17 (9) -release (x86_64-unknown-cygwin))

for the first (3.1) for nn in (00..99) ; do ... for nn in (00..99) ; do ... works, but for nn in (000..999) ; do ... for nn in (000..999) ; do ... does not work both will work on version 4.1; did not test printf behavior ( bash --version provided version information)

Greetings, Yang

+1
Feb 05 '15 at 18:11
source share



All Articles