< BACK

Bash tips, tricks and code snippets

Increment a variable

In Bash there are several ways to achieve this

var=$((var+1))
((var=var+1))
((var+=1))
((var++))
((++var))
let "var+=1"
let "var++"
let "++var"

Note: using - has the exact opposite effect of decrementing the variable:

var=$((var-1))
((var=var-1))
((var-=1))
((var--))
((--var))
let "var-=1"
let "var--"
let "--var"