Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. $((Arithmetic Expansion))

$((Arithmetic Expansion))

Bash's arithmetic expansion $(( )) lets you perform integer arithmetic, comparisons, and bitwise operations using only shell built-ins. It is faster and more readable than the external expr command.

Syntax

Arithmetic expansion (returns the result as a string).

result=$(( expression ))

Arithmetic command (returns true/false via exit status).

(( expression ))

Using the let command.

let "variable=expression"

Legacy external command (not recommended).

result=$(expr expression)

Operators

OperatorDescription
+ - * / %Addition, subtraction, multiplication, integer division, and remainder.
**Exponentiation (e.g., 2**10 = 1024).
++ --Increment and decrement (both prefix and postfix forms are supported).
+= -= *= /= %=Compound assignment operators.
== != < > <= >=Comparison operators (return 1 for true, 0 for false).
&& ||Logical AND and OR.
! ~ & | ^ << >>Logical NOT and bitwise operations (NOT, AND, OR, XOR, and shift).
condition ? true : falseTernary operator.

Sample Code

Basic arithmetic operations. Inside $(( )), you can reference variables without the $ prefix.

calc.sh
a=10
b=3
echo "Addition:       $(( a + b ))"
echo "Subtraction:    $(( a - b ))"
echo "Multiplication: $(( a * b ))"
echo "Division:       $(( a / b ))"
echo "Remainder:      $(( a % b ))"
bash calc.sh
Addition:       13
Subtraction:    7
Multiplication: 30
Division:       3
Remainder:      1

Use ** for exponentiation.

echo "2 to the power of 10: $(( 2**10 ))"
2 to the power of 10: 1024

Use increment and compound assignment operators to update a variable's value.

increment.sh
count=0
(( count++ ))
echo "After increment: $count"

(( count += 5 ))
echo "After +=: $count"
bash increment.sh
After increment: 1
After +=: 6

(( )) can also be used in conditionals. A non-zero result returns exit status 0 (true).

compare.sh
num=7
if (( num > 5 )); then
    echo "$num is greater than 5"
fi
bash compare.sh
7 is greater than 5

You can also enter an if statement directly in the terminal. After pressing Enter after then, a > prompt appears — this means the shell is waiting for more input. Enter fi to execute.

if (( 7 > 5 )); then
    echo "7 is greater than 5"
fi
7 is greater than 5

Use a C-style for loop to iterate with a counter.

sum.sh
sum=0
for (( i=1; i<=10; i++ )); do
    (( sum += i ))
done
echo "Sum of 1 to 10: $sum"
bash sum.sh
Sum of 1 to 10: 55

You can also enter a for loop directly in the terminal. After pressing Enter after do, a > prompt appears — this means the shell is waiting for more input. Enter done to execute.

sum=0; for (( i=1; i<=10; i++ )); do
    (( sum += i ))
done
echo "Sum of 1 to 10: $sum"
Sum of 1 to 10: 55

The ternary operator is also available.

ternary.sh
x=15
result=$(( x > 10 ? x * 2 : x + 10 ))
echo "result: $result"
bash ternary.sh
result: 30

Bitwise operations (AND and shift).

bitwise.sh
echo "Bitwise AND: $(( 0b1010 & 0b1100 ))"
echo "Left shift:  $(( 1 << 4 ))"
bash bitwise.sh
Bitwise AND: 8
Left shift:  16

Notes

Bash arithmetic supports integers only. For decimal calculations, use the bc command (e.g., $ echo "scale=2; 10/3" | bc → 3.33).

Inside $(( )), you can reference variables without the $ prefix (adding it also works). String variables and undefined variables are treated as 0.

For details on command substitution ($()), see Command Substitution.

If you find any errors or copyright issues, please .