As the doctor gone rogue

September 8, 2016

value too great for base ?!

Filed under: bash — hypotheses @ 11:11 pm

So, I wrote a script trying to get it to output the previous month as a number with a leading “0”. The problem arrives as bash complains of some errors.

Initially, my script looked like this

MO=$(date '+%m')
MO2=$((MO-1))
P_MONTH=$(printf "%0*d\n" 2 $MO2);

The last line is there so that I still get a numeric month with a leading zero. However, this for some reasons, gave the error “value too great for base”.

After trying to figure out what it actually means, the base here is the numeric base, which bash thinks number with leading “0” is octal.
So, the solution to fix this is to reassure that the ${MO} variable, which is the numeric Month is a base-10 number.

You can achieve this as follow

MO=$(date '+%m');MO=$((10#$MO))
MO2=$((MO-1))
P_MONTH=$(printf "%0*d\n" 2 $MO2);

Blog at WordPress.com.