0

I am trying to iterate a sequence of numbers , the postal codes in Spain is from 01001 to 52080 and I have the following code.

#!/bin/bash
for (( c=01001; c<=52080; c++ ))
do  
    echo "$c"
done

But I have a problem , I need to add a 0 when the length of the numbers are four.

How can I do this ?

{
      "countryName" : "Spain" ,
      "numPostalCodes" : 37866 ,
      "countryCode" : "ES" ,
      "maxPostalCode" : "52080" ,
      "minPostalCode" : "01001"
    } 

Regards !

jc1992
  • 644
  • 3
  • 10
  • 24
  • 1
    You can do this with the `printf` builtin. See [here](https://stackoverflow.com/questions/8789729/how-to-zero-pad-a-sequence-of-integers-in-bash-so-that-all-have-the-same-width) for more info. – jpaugh Feb 15 '18 at 21:46
  • Correct , sorry for the question I can delete the question if you want. – jc1992 Feb 15 '18 at 21:50
  • It's actually helpful to have a question phrased in different ways, as long as each question points to the same set of answers. This will probably get closed as a duplicate. In the meantime, I'm glad you have found your answer! – jpaugh Feb 15 '18 at 21:55

1 Answers1

1

Just use printf instead of echo:

printf "%05d\n" "$c"
Jdamian
  • 3,015
  • 2
  • 17
  • 22