Friday, September 4, 2015

How to handle a leading zero in Bash

If you have a leading zero in a number,

the number is handled as octal number as follows:

$ echo $((08 + 1))
-bash: 08: value too great for base (error token is "08")
$

To handle it as decimal number you have to do as follows:

$ echo $((10#08 + 1))
9
$

Reference:
http://blog.famzah.net/2010/08/07/beware-of-leading-zeros-in-bash-numeric-variables/

No comments:

Post a Comment