Integer गणना
+ सिर्फ strings को glue करेगा।In this page:
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 है।
$((7 / 2)) 3 है, 3.5 नहीं।उदाहरण: Arithmetic Expansion: $(( ))
#!/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"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
count=0
(( count = count + 1 ))
echo "count is now $count"
if (( count > 0 )); then
echo "count is positive"
fi
Login to try C/C++/Java/PHP code in the editor
The let Builtin
let $(( )) जैसा ही arithmetic assignment perform करता है, जो आपको x=$((x + 1)) की बजाय let x=x+1 लिखने देता है। यह older style है और modern scripts में कम उपयोग होता है, लेकिन आप इसे existing code में अब भी देखेंगे।
उदाहरण: The let Builtin
#!/bin/bash
x=10
let x=x+5
let "x -= 2"
echo "x is now $x"
Login to try C/C++/Java/PHP code in the editor
Increment/Decrement Shortcuts
(( )) या $(( )) के अंदर, Bash +=, -=, ++, और -- जैसे C-style shortcuts support करता है एक variable को जगह पर update करने के लिए। ये purely arithmetic-context conveniences हैं और double parentheses के बाहर वैसे काम नहीं करते।
उदाहरण: Increment/Decrement Shortcuts
#!/bin/bash
score=0
(( score += 10 ))
(( score++ ))
echo "Final score: $score"
Login to try C/C++/Java/PHP code in the editor
- Plain string concatenation से math करने की कोशिश करना, जैसे
total=$a+$b, जो बस literal text 'a+b' store करता है, कुछ calculate करने के बजाय। - यह भूल जाना कि Bash arithmetic default रूप से integer-only है;
$(( 7 / 2 ))3देता है,3.5नहीं, क्योंकि इसमें कोई built-in floating-point division नहीं है (इसके लिए आपकोbcयाawkचाहिए)। 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 में उपयोग होता है।letarithmetic assignment perform करने का एक older, कम-उपयोग होने वाला तरीका है, जैसेlet x=x+1।- ये तीनों forms सिर्फ integer math support करते हैं; division zero की ओर truncate होता है और कोई native floating point नहीं है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: