The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 10,795β€…    Votes:  2β€…
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: November 24, 2022    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.

⇧ HTML5 Videos flickering Editing OS names in /etc/default/grub - where is the OS name read from?  β‡©