Reputation: 2663
Iam doing scripting using bash where I get the negative number like this
-001 , -002 , -003 , ........................., -008 , -009 , -010 , -011 , -012 .....
I have to change them to positive number so I decided to multiply -1 with them. Then
$ val=$(( -1*-001 ))
$ echo $val
$ 1
$ val=$(( -1*-002 ))
$ echo $val
$ 2
Result are fine up to number -007 but when I multiply with -008 and -009 then error occur as below
$ val=$(( -1*-008 ))
bash: -1*-008: value too great for base (error token is "008")
$ val=$(( -1*-009 ))
bash: -1*-009: value too great for base (error token is "009")
Another strange behaviour is that when I multiply with -010, -011 ,-012 and so on... unusual result occur like below
$ val=$(( -1*-010 ))
$ echo $val
$ 8
$ val=$(( -1*-011 ))
$ echo $val
$ 9
$ val=$(( -1*-012 ))
$ echo $val
$ 10
$ val=$(( -1*-013 ))
$ echo $val
$ 11
and so on............
Why this happens?
Upvotes: 2
Views: 1472
Reputation: 1385
You can force decimal (instead of octal) values with 10# prefix :
$ val=$(( -1*-10#008 ))
$ echo $val
8
Upvotes: 6
Reputation: 239382
Leading zeros denote numbers in octal. 010 and 10 are not the same number; the first is octal: 010 octal is 8 in decimal. Similarly, "009" isn't a real number, hence the error you're seeing: there is no digit "9" in octal.
You need to strip leading zeros.
Upvotes: 8