Why does bash think the number 010 = 8?
x=010
echo $x
010
echo $(( x+0 ))
8
echo $(( x-2 ))
6
I thought it might be binary, but 010 = 10 = 2. So why does it get 8, and how can I make it think 010 = 10 (and 010 - 2 = 8)?
Why does bash think the number 010 = 8?
x=010
echo $x
010
echo $(( x+0 ))
8
echo $(( x-2 ))
6
I thought it might be binary, but 010 = 10 = 2. So why does it get 8, and how can I make it think 010 = 10 (and 010 - 2 = 8)?
Number sequences starting with a
0
are interpreted as octal numbers.Octal
10
= decimal8
.To get bash to treat it as a decimal number, remove the leading zero or force decimal with:
Generally that works for all bases, just replace the
10
with the base that you want: