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

Increment और Decrement

Increment and decrement एक counter को एक-एक बार click up या down करने जैसा है जब कोई आता है। आप बड़े steps में भी jump कर सकते हैं, जैसे एक साथ पांच जोड़ना।

In this page:

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

Increment and Decrement

Bash increment/decrement के लिए ((x++)) और ((x--)) support करता है, साथ ही ((x += 5)) जैसी compound assignment। ये सिर्फ (( )) जैसे arithmetic contexts के अंदर काम करते हैं, plain commands के रूप में नहीं। ये एक loop में counter update करने का concise तरीका हैं।

Note: ((x++)) x की *original* value के आधार पर exit status return करता है — अगर x 0 था, तो expression खुद set -e purposes के लिए "false" evaluate होता है।

उदाहरण: 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. #}
आम गलतियां
  1. i++ को एक command के रूप में अकेला उपयोग करना, जो valid नहीं है; ((i++)) या i=$((i+1)) उपयोग करें।
  2. यह न जानना कि ((i++)) value 0 होने पर exit status 1 return करता है, जो set -e के under fail होता है।
  3. Spaces के साथ i = i + 1 लिखना, जो एक 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.