🔍 See Original Answer on Ask Ubuntu 🔗
Adding 1 to a variable doesn't work as expected (Bash arithmetic)
September 11, 2016
November 24, 2022
Copy to clipboard: false
An alternate method may be to keep your variables as integers and convert them to a string at the end:
A=12 B=$((A+1)) echo $B 13 C=$( printf '%04d' $B ) echo $C 0013
This style of working with integers in math and converting to string for the answer is more intuitive to me as I’m used to BASIC programming. I appreciate Bash doesn’t have variable typing like C and BASIC but pretending it does makes me happy.