Why did “09” and “08” are invalid numbers, “07” and “06” are fine
Because preceding integer by 0
is a conventional shortcut meaning number is octal.
printf "%d\n" 010 0100
8
64
printf "%o\n" 8 64 0100
10
100
100
printf "%04o\n" 8 64 0100
0010
0100
0100
How to work with this?
Using bash integer: preceding number by 10#
Under bash, you could precise by this way, which base is used for number
echo $(( 10#0100))
100
echo $(( 10#0900))
900
echo $(( 10#0900 ))
900
and
echo $(( 8#100 ))
64
echo $(( 2#100 ))
4
Of course!
There are only 10 types of people in the world: those who understand binary, and those who don't.
Using bash Parameter Expansion
For this, you have to use a variable
a=09
echo ${a#0}
9
This will work fine until you just have only one 0
to drop.
a=0000090
echo ${a#*0}
000090
echo ${a##0}
000090
For this, you coul use extglob
feature:
echo ${a##*(0)}
0000090
shopt -s extglob
echo ${a##*(0)}
90
Using floating point
with printf
printf "%.0f\n" 09
9
I like using -v
option of printf
for setting some variable:
printf -v a %.0f 09
echo $a
9
or even if
a=00090
printf -v a %.0f $a
echo $a
90
0
mean octal representation. Try:printf %d\\n 070
and/orprintf %d\\n 010