Why does `$(( 08 + 1 ))` fail, and how do you fix it?
AdvancedConceptVariables, quoting and expansions
Answer
A leading zero means octal in shell arithmetic, and 8 and 9 are not octal digits: bash: 08: value too great for base (error token is "08"). This bites when doing math on date +%m, date +%H or zero-padded IDs.
month=$(date +%m) # "08"
next=$(( 10#$month + 1 )) # force base 10 -> 9
hour=$(date +%-H) # GNU date: no padding
More on Variables, quoting and expansions
What are the rules for assigning variables in Bash?BeginnerWhat is the difference between single quotes, double quotes and `$'...'`?BeginnerWhy should you almost always quote variable expansions? Show what goes wrong.BeginnerExplain the difference between `"$@"`, `"$*"`, `$@` and `$*`.IntermediateWhat do `${var:-default}`, `${var:=default}`, `${var:+alt}` and `${var:?msg}` do?IntermediateHow do you strip prefixes and suffixes with parameter expansion?Intermediate