Interview Prep/Shell Scripting/Variables, quoting and expansions

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
All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Variables, quoting and expansions