Increment and Decrement
In this page:
Increment and Decrement
Bash supports ((x++)) and ((x--)) for increment/decrement, plus compound assignment like ((x += 5)). These only work inside arithmetic contexts like (( )), not as plain commands. They're the concise way to update a counter in a loop.
Note: ((x++)) returns the exit status based on the *original* value of x — if x was 0, the expression itself evaluates as "false" for set -e purposes.
Example: Increment and Decrement
#!/bin/bash
count=0
((count++))
((count++))
((count += 5))
echo "Count: $count"
((count--))
echo "After decrement: $count"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: