Views:
13,557β
Votes: 3β
Tag :
bash
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/823649
Title:
Adding 1 to a variable doesn't work as expected (Bash arithmetic)
ID:
/2016/09/11/Adding-1-to-a-variable-doesn_t-work-as-expected-_Bash-arithmetic_
Created:
September 11, 2016
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
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.