HOWTO · Linux
How to Perform Increment and Decrement Operation in Bash
Learn how to perform increment and decrement operations in Bash with our comprehensive guide. Discover various methods including arithmetic and unary operators, complete with clear examples. Enhance your Bash scripting skills and streamline your code with these essential techniques.
On this page
Increment and decrement operations change an integer variable by a fixed amount. In Bash, arithmetic expansion, arithmetic commands, let, compound assignments, and unary operators all support these updates. Choose the form that makes the amount and the value you use afterward easiest to read.
Increment and decrement with + and -
Arithmetic expansion, written $((...)), produces a value you can assign or print. An arithmetic command, ((...)), evaluates an expression and can update variables directly. The let builtin also evaluates arithmetic expressions; quote its expression so the shell passes it as one argument.
Run this example:
x=3
y=3
z=3
x=$((x + 1))
((y = y + 1))
let "z=z+1"
printf 'x: %s\ny: %s\nz: %s\n' "$x" "$y" "$z"
x=$((x - 1))
((y = y - 1))
let "z=z-1"
The first three updates add one using different arithmetic forms; the last three subtract one. Arithmetic expansion is useful when an expression’s value is needed, while arithmetic commands and let are convenient for updates. Bash arithmetic uses integer values.
Use += and -= for arithmetic updates
The compound assignments x+=amount and x-=amount add or subtract the right-hand integer from the variable on the left. They correspond to x=x+amount and x=x-amount, so use them when the update amount is more than one or should be obvious at a glance.
count=10
((count += 3))
printf 'after adding 3: %s\n' "$count"
((count -= 2))
printf 'after subtracting 2: %s\n' "$count"
These expressions change count in place. The arithmetic command’s status is based on whether its expression evaluates to zero, so avoid relying on that status as a success indicator for an update.
Use ++ and -- for one-step changes
The unary operators ++ and -- add or subtract exactly one. As standalone arithmetic commands, ((count++)), ((++count)), ((count--)), and ((--count)) update the variable.
When an expression uses the operator’s value, prefix and postfix differ: ++count updates first and yields the new value; count++ yields the old value and then updates. The same ordering applies to --. If only the resulting variable matters, place the update in its own statement to avoid ambiguity.
count=4
old=$((count++))
printf 'old: %s, count: %s\n' "$old" "$count"
new=$((++count))
printf 'new: %s, count: %s\n' "$new" "$count"
Use increments and decrements in loops
A loop’s condition determines when it stops, and its update moves the counter toward that stopping point. Keep those pieces consistent: an increasing counter needs an upper-bound condition, while a decreasing counter needs a lower-bound condition.
for ((count = 1; count <= 3; count++)); do
printf '%s\n' "$count"
done
count=3
while ((count >= 1)); do
printf '%s\n' "$count"
((count--))
done
The for loop groups initialization, condition, and increment in its header. The while loop puts its condition before the body and its decrement inside the body. Make sure every loop update eventually makes its condition false.
Bash arithmetic limits and common questions
Bash arithmetic is integer-based and does not provide native floating-point calculations. For decimal arithmetic, use a suitable external calculator or a language with floating-point support, and account for that tool as a separate dependency.
++ and += 1 both increase a variable by one. The unary form is concise; compound assignment makes the amount explicit and also works for larger updates. Prefix and postfix matter only when the expression’s value is used, as described above.
An arithmetic command returns status 1 when its expression evaluates to zero and status 0 otherwise. For ((count--)), the expression uses the value before decrement: changing count from 1 to 0 returns status 0, while starting at 0 returns status 1 even though the variable becomes -1. For ((--count)) and ((count -= 1)), the expression uses the value after decrement, so changing 1 to 0 returns status 1. This difference matters when an arithmetic command is used as an if or while condition, or as a simple command with set -e, where a nonzero status can affect control flow. In the decrementing loop above, ((count--)) returns status 0 for the 1-to-0 update; the next explicit ((count >= 1)) condition ends the loop.