Bash Arithmetic $ number! = $ ((Number))

When trying to run a simple bash script to increase the number with the previous 0 by 1, the original number is interpreted incorrectly.

#!/bin/bash number=0026 echo $number echo $((number)) echo $((number+1)) 

When this command is executed, I get the output:

 0026 22 23 

Why is this happening?

+6
source share
1 answer

To force a base-10 view:

 $ echo $((10#$number)) 26 $ echo $((10#$number + 1)) 27 

Responding to kojiro's comment:

 $ something=08 $ echo $((something)) bash: 08: value too great for base (error token is "08") $ echo $(($something)) bash: 08: value too great for base (error token is "08") $ echo $((10#something)) bash: 10#something: value too great for base (error token is "10#something") $ echo $((10#$something)) 8 $ echo $((08)) bash: 08: value too great for base (error token is "08") $ echo $((10#08)) 8 $ echo $(( 16#10 )) 16 $ echo $(( 16#f )) 15 $ echo $(( 16#10 - 1 )) 15 
+4
source

All Articles