How to increment the value of a (decimal) variable (with leading zero) by +1?

The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.

To force the use of decimal, add 10# (as long as the number has no leading sign):

BN=10#$(cat Build.number)
echo $((++BN)) > Build.number

To print the number using at least three digits, use printf:

printf "%.3d\n" $((++BN)) > Build.number

bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.

That's actually a POSIX requirement.

Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.

With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.

In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.


In any POSIX shell, you can prevent a number from being considered octal by stripping its leading zeros with a combination of the ${var#prefix} and ${var%%suffix} expansion forms:

BN=001002; BN=$(( ${BN#${BN%%[!0]*}} + 1 )); echo "$BN"
1003

In shells which support the ${var//pat/repl} syntax, you can also do that by prepending it a 1 and subtracting it from 10^{number_of_digits}:

BN=000012; BN=$(( 1$BN - 1${BN//?/0} )); echo "$BN"; BN=$((BN+1)); echo "$BN"
12
13

This works in bash, zsh, ksh93, mksh and yash.

In bash, ksh93 and zsh (but not in yash and mksh) you can also use the fortranish ** operator (exponentiation):

BN=000012; BN=$(( 1$BN - 10**${#BN} ))