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

Increment and Decrement

Increment and decrement is like clicking a counter up or down by one each time someone walks in. You can also jump by bigger steps, such as adding five at once.

In this page:

  1. Increment and Decrement
Syntax
bash
((variable++))
((variable--))
((variable += value))

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"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using i++ alone as a command, which is not valid; use ((i++)) or i=$((i+1)).
  2. Not knowing that ((i++)) returns exit status 1 when the value was 0, which fails under set -e.
  3. Writing i = i + 1 with spaces, which is not an assignment.

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.