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

Arithmetic Expansion $(( )) का उपयोग

Arithmetic expansion एक built-in calculator जैसा है जो आप किसी भी sentence में drop कर सकते हैं। आप sum को double parentheses में लिखते हैं और answer इसकी जगह ले लेता है।
Syntax
bash
result=$(( operand1 operator operand2 ))

Arithmetic Expansion $(( ))

$(( expression )) एक integer arithmetic expression को directly evaluate करता है, operators के आस-पास spaces या किसी external command की ज़रूरत के बिना। यह + - * / % और comparison/logical operators support करता है, सब normal integer math उपयोग करते हुए।

यह Bash में arithmetic करने का standard, modern तरीका है।

Note: $(( )) सिर्फ integers support करता है — Bash में कोई built-in floating-point arithmetic नहीं है।

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

bash
#!/bin/bash
a=10
b=3

echo "Sum: $((a + b))"
echo "Product: $((a * b))"
echo "Integer division: $((a / b))"
echo "Remainder: $((a % b))"
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. एक assignment में = के आस-पास spaces लगाना, जैसे x = $((1+2))।
  2. $(( 5 / 2 )) से 2.5 मिलने की उम्मीद करना, जब Bash arithmetic integers उपयोग करता है और 2 देता है।
  3. Non-numeric text पर $ उपयोग करना, जिसे zero माना जाता है या error मिलती है।

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.