$((Arithmetic Expansion))
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 2.0(1996) |
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
| Operator | Description |
|---|---|
| + - * / % | 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 : false | Ternary operator. |
Sample Code
Basic arithmetic operations. Inside $(( )), you can reference variables without the $ prefix.
sample_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 ))"
Run the following command:
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.
sample_increment.sh
count=0 (( count++ )) echo "After increment: $count" (( count += 5 )) echo "After +=: $count"
Run the following command:
bash increment.sh After increment: 1 After +=: 6
(( )) can also be used in conditionals. A non-zero result returns exit status 0 (true).
sample_compare.sh
num=7
if (( num > 5 )); then
echo "$num is greater than 5"
fi
The following example demonstrates this:
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.
sample_sum.sh
sum=0
for (( i=1; i<=10; i++ )); do
(( sum += i ))
done
echo "Sum of 1 to 10: $sum"
The following example demonstrates this:
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.
sample_ternary.sh
x=15 result=$(( x > 10 ? x * 2 : x + 10 )) echo "result: $result"
The following example demonstrates this:
bash ternary.sh result: 30
Bitwise operations (AND and shift).
sample_bitwise.sh
echo "Bitwise AND: $(( 0x0A & 0x0C ))" echo "Left shift: $(( 1 << 4 ))"
Run the following command:
bash bitwise.sh Bitwise AND: 8 Left shift: 16
Common Mistakes
Common Mistake 1: Adding spaces around the operator causes an error
Inside $(( )), spaces around operators are fine. However, spaces in the assignment itself cause a command-not-found error.
result = $(( 1 + 2 )) bash: result: command not found
Write the assignment without spaces around =.
result=$(( 1 + 2 )) echo $result 3
Common Mistake 2: Trying to compute floating-point numbers
$(( )) handles integers only. Division is integer division and fractional parts are discarded.
echo $(( 10 / 3 )) 3
Use bc for floating-point arithmetic.
echo "scale=2; 10 / 3" | bc 3.33
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 contact us.