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

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.
Syntax
bash
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 $(( ))

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. #}
Common Mistakes
  1. Putting spaces around = in an assignment, as in x = $((1+2)).
  2. Expecting $(( 5 / 2 )) to give 2.5, when Bash arithmetic uses integers and gives 2.
  3. Using $ on non-numeric text, which is treated as zero or gives an 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.