Arithmetic Expansion $(( ))
Arithmetic expansion is like a built-in calculator you can drop into any sentence. You write the sum in double parentheses and the answer replaces it.
In this page:
Syntax
result=$(( operand1 operator operand2 ))
Arithmetic Expansion $(( ))
$(( expression )) evaluates an integer arithmetic expression directly, without needing spaces around operators or an external command. It supports + - * / % and comparison/logical operators, all using normal integer math.
This is the standard, modern way to do arithmetic in Bash.
Note:
$(( )) only supports integers — there is no built-in floating-point arithmetic in Bash.Example: Arithmetic Expansion $(( ))
#!/bin/bash
a=10
b=3
echo "Sum: $((a + b))"
echo "Product: $((a * b))"
echo "Integer division: $((a / b))"
echo "Remainder: $((a % b))"
Login to try C/C++/Java/PHP code in the editor
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
- Putting spaces around
=in an assignment, as inx = $((1+2)). - Expecting
$(( 5 / 2 ))to give2.5, when Bash arithmetic uses integers and gives2. - Using
$on non-numeric text, which is treated as zero or gives an error.
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: