Bash - Prepend zero to single digit in while loop

5,480

Solution 1

Using your script, the following will work.

#!/bin/bash
x=0
ep=1
while [ $x -le 11 ]
do
  ep_padded=$(printf '%02d' $ep)
  echo "Welcome $x times"
  date --date="$x week" +"%Y:%m:%d"
  exiftool -exififd:dateTimeOriginal="$(date --date="$x week" +"%Y:%m:%d") 00" $1$ep_padded*
  x=$(( $x + 1 ))
  ep=$(( $ep + 1 ))
done

You can use the following form to increment your variables too

x=$((++x))
ep=$((++ep))

Solution 2

You can use shell parameter expansion to add zero-padding to numbers.

For example, to print number in a 5-digits format:

$ zeroos="00000"
$ number="123"

$ temp_num="$zeroos$number"
$ echo "${temp_num:(-5)}"
00123

# Or instead of the "5" constant, use $zeroos length
$ echo "${temp_num:(-${#zeroos})}"    
00123

Update:

Above example cuts off the front-most digits of $number, when the number becomes longer (with more digits) than $zeroos:

$ number="1234567" # Changed to 7 digits
echo "${temp_num:(-5)}"
34567 # Resulted in a wrong number!

A safer way:

$ number="123"
$ echo "${zeroos:${#number}:${#zeroos}}${number}"
00123

$ number="1234567"
$ echo "${zeroos:${#number}:${#zeroos}}${number}"
1234567

Note 1:

A slightly different variant, to use the first digits of $zeroos resp. $century (instead of the last ones) would be:

$ century="2000"
$ year="1"
$ echo "${century:0:-${#year}}${year}"
2001

This comes in handy for example, if you have mixed dates with 1-, 2- or even 4-digit years, but want them to be 4 digits in any case, without the need of conditions (for checking their actual digits) and any arithmetic.
The downside is, if ${#year} gets greater than ${#century}, this results in an error.

Note 2:

When using printf (as suggested on other answers), be aware that it could be mush slower on a large scale (see an example).

Solution 3

set your variable as below, this will add a padding zero, you can add more zeros in case of your expectations.

ep="$(printf '%02d' $((++ep)) )"
Share:
5,480

Related videos on Youtube

Solaris
Author by

Solaris

College student Likes cli more than gui

Updated on September 18, 2022

Comments

  • Solaris
    Solaris over 1 year

    I made this script exif_script And want to change ep variable to double digit Eg 01 instread of 1.

    #!/bin/bash
    x=0
    ep=1
    while [ $x -le 11 ]
    do
      echo "Welcome $x times"
      date --date="$x week" +"%Y:%m:%d"
      exiftool -exififd:dateTimeOriginal="$(date --date="$x week" +"%Y:%m:%d") 00" $1$ep*
      x=$(( $x + 1 ))
      ep=$(( $ep + 1 ))
    done
    
    • Solaris
      Solaris almost 7 years
      @SergiyKolodyazhnyy i had some jpg files named test1.jpg test2.jpg and it seems to work but my main files like test01 test02 ..... and so i cant add 0 in hard code cause it will make 011 instead of 11
    • Arronical
      Arronical almost 7 years
      @Solaris use '%02d' as the string in the command by AFSHIN.
    • Solaris
      Solaris almost 7 years
      @Arronical thnx it seems to be working great now , tho i got some warning for '08 value is too great' but the script is doing its work
    • Arronical
      Arronical almost 7 years
      Ah the warning might be because of the shell arithmetic when you +1 to ep. If there are leading 0s in shell arithmetic, it treats the number as hexadecimal I think. I'd use a separate variable for the display version of ep (maybe ep_padded="$(printf '%02d' $ep)) then increment the non-padded version.
    • Solaris
      Solaris almost 7 years
      @Arronical it i tired your suggestion but my files get only updated till 07 , error = ./loop: line 7: printf: 08: invalid octal number Welcome 8 times 00 ./loop: line 6: 08: value too great for base (error token is "08")
    • Solaris
      Solaris almost 7 years
      @Arronical files after and including 08 dont get updated , instead it starts again from 00 ,
  • Solaris
    Solaris almost 7 years
    Thnx also sorry for duplicate thread , i couldnt find option to close or delete the post
  • Solaris
    Solaris almost 7 years
    It doesnt seem to be working for me , and as i checking browsing though other posts , maybe printf is causing problem
  • Arronical
    Arronical almost 7 years
    The printf command needs '%02d' as its formatting string.
  • Solaris
    Solaris almost 7 years
    Tried it but still getting error at 08 and i figured out it was cause every number starting with 0 in bash is considered octal , while 08 is not a vaild octal number, i got it fixed by explicitly declaring base 10 like $((10#$x)) instead of $x
  • Arronical
    Arronical almost 7 years
    The x variable should at no point have a padding of 0 in front of it, the code you're using must be different from what's in this answer.
  • Solaris
    Solaris almost 7 years
    yea sorry i messed up a lil while testing , thnx your code is working perfect , i am sorry for my mistakes and lack of knowledge
  • Arronical
    Arronical almost 7 years
    Don't worry @Solaris everybody has to start learning somewhere. Learning about specifying the base in bash arithmetic is a useful lesson too, so you've actually learned more through the little mistakes!
  • Solaris
    Solaris almost 7 years
    i only learned cause this community helps me , i know c++ but errors without help of compiler are hard to stop , anyways thnx for giving you time to solve my probelms , i will try to so the same for someome else
  • DJCrashdummy
    DJCrashdummy over 3 years
    this answer (especially the 2nd variant) is pure genius: short, simple & efficient! - no clue why this isn't the accepted answer!?! Parameter Expansion is surprisingly quite unknown. :-(