← Back to Bash Course | Chapter 4: Loops | Lesson 11 of 14

Increment and Decrement

In this page:

  1. Increment and Decrement

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

bash
#!/bin/bash
count=0

((count++))
((count++))
((count += 5))

echo "Count: $count"

((count--))
echo "After decrement: $count"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.