← Back to Bash Course | Chapter 2: Variables & Types | Lesson 4 of 13

Integer गणना

Bash कुछ special notations उपयोग करके whole-number math कर सकता है, क्योंकि normal variables सिर्फ text हैं और plain + सिर्फ strings को glue करेगा।
Syntax
bash
result=$(( expression ))
(( variable = expression ))
let variable=expression

Arithmetic Expansion: $(( ))

$(( expression )) एक C-like arithmetic expression evaluate करता है और resulting integer में expand होता है, जिसे एक variable में store किया जा सकता है या directly text में उपयोग किया जा सकता है।

$(( )) के अंदर आप variables को एक leading $ के बिना refer कर सकते हैं, हालांकि इसे उपयोग करना भी allowed है।

Warning: Integer division zero की ओर truncate होता है: $((7 / 2)) 3 है, 3.5 नहीं।

उदाहरण: Arithmetic Expansion: $(( ))

bash
#!/bin/bash
a=7
b=3
sum=$((a + b))
product=$((a * b))
quotient=$((a / b))
remainder=$((a % b))
echo "sum=$sum product=$product quotient=$quotient remainder=$remainder"

Side Effects के लिए ((...))

(( expression )) command (कोई leading $ नहीं) expression को सिर्फ इसके exit status और side effects के लिए evaluate करता है, जैसे एक variable increment करना, output produce करने के बजाय।

इसका exit status 0 (true) है अगर expression एक non-zero number evaluate हो, और 1 (false) अगर यह zero evaluate हो।

उदाहरण: ((...)) for Side Effects

bash
#!/bin/bash
count=0
(( count = count + 1 ))
echo "count is now $count"
if (( count > 0 )); then
    echo "count is positive"
fi

The let Builtin

let $(( )) जैसा ही arithmetic assignment perform करता है, जो आपको x=$((x + 1)) की बजाय let x=x+1 लिखने देता है। यह older style है और modern scripts में कम उपयोग होता है, लेकिन आप इसे existing code में अब भी देखेंगे।

उदाहरण: The let Builtin

bash
#!/bin/bash
x=10
let x=x+5
let "x -= 2"
echo "x is now $x"

Increment/Decrement Shortcuts

(( )) या $(( )) के अंदर, Bash +=, -=, ++, और -- जैसे C-style shortcuts support करता है एक variable को जगह पर update करने के लिए। ये purely arithmetic-context conveniences हैं और double parentheses के बाहर वैसे काम नहीं करते।

उदाहरण: Increment/Decrement Shortcuts

bash
#!/bin/bash
score=0
(( score += 10 ))
(( score++ ))
echo "Final score: $score"
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. Plain string concatenation से math करने की कोशिश करना, जैसे total=$a+$b, जो बस literal text 'a+b' store करता है, कुछ calculate करने के बजाय।
  2. यह भूल जाना कि Bash arithmetic default रूप से integer-only है; $(( 7 / 2 )) 3 देता है, 3.5 नहीं, क्योंकि इसमें कोई built-in floating-point division नहीं है (इसके लिए आपको bc या awk चाहिए)।
  3. let उपयोग करना बिना यह realize किए कि इसका exit status यह reflect करता है कि *result* zero था या non-zero, न कि command syntax succeed हुआ या नहीं, जो &&/|| chains को trip कर सकता है।
चैप्टर सारांश
  • $(( expression )) एक arithmetic expression evaluate करता है और numeric result को text के रूप में return करता है।
  • ((...)) (बिना leading $) expression को इसके side effects/exit status के लिए evaluate करता है, जो commonly conditionals और loops में उपयोग होता है।
  • let arithmetic assignment perform करने का एक older, कम-उपयोग होने वाला तरीका है, जैसे let x=x+1।
  • ये तीनों forms सिर्फ integer math support करते हैं; division zero की ओर truncate होता है और कोई native floating point नहीं है।

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.