Arithmetic in Bash — Integer Math and Calculations
Bash handles integer arithmetic natively with $(( )) and let. Learn how to count, compare, and do math in your scripts — and when to reach for bc for decimals.
Bash is an integer-only language — it has no native floating-point math. Everything inside $(( )) is treated as integer arithmetic. This is enough for counters, array indices, byte calculations, loop limits, and most scripting math. For decimal calculations you'll use bc or awk.
The $(( )) Arithmetic Expansion
# Basic operations
echo $(( 5 + 3 )) # 8
echo $(( 10 - 4 )) # 6
echo $(( 3 * 7 )) # 21
echo $(( 17 / 5 )) # 3 (integer division — truncates)
echo $(( 17 % 5 )) # 2 (modulo — remainder)
echo $(( 2 ** 10 )) # 1024 (exponentiation)
# Variables don't need $ inside (( )), but it works either way
x=10
y=3
echo $(( x * y )) # 30
echo $(( $x + $y )) # 13 (also fine)Assigning Arithmetic Results
# Assign with $(( ))
count=$(( 5 + 3 ))
echo "$count" # 8
# In-place modification
count=$(( count + 1 )) # increment
count=$(( count * 2 )) # double
# Compound assignment operators — shorter
(( count++ )) # increment by 1 (like C)
(( count-- )) # decrement by 1
(( count += 5 )) # add 5 in place
(( count *= 2 )) # multiply in place
(( count **= 2 )) # square in place
# (( )) as a statement (no $ needed when you don't capture output)
(( count = 10 )) # sets count to 10$(( )) vs (( ))
$(( expr )) — arithmetic expansion: evaluates expr and substitutes the result as a string. Use when you need the value: result=$(( 2 + 2 )).
(( expr )) — arithmetic command: evaluates expr for its exit status (0=non-zero result, 1=zero result). Use for conditions and side effects: (( count++ )) or if (( count > 10 )); then.
Arithmetic in Conditions
count=15
# Using (( )) in if
if (( count > 10 )); then
echo "count is greater than 10"
fi
# Comparison operators inside (( ))
(( a == b )) # equal
(( a != b )) # not equal
(( a < b )) # less than
(( a <= b )) # less than or equal
(( a > b )) # greater than
(( a >= b )) # greater than or equalFloating-Point Math with bc
When you need decimal results, pipe through bc (basic calculator):
# Basic decimal
echo "scale=2; 22 / 7" | bc # 3.14
# Square root
echo "scale=4; sqrt(2)" | bc # 1.4142
# Using variables in bc
radius=5
area=$(echo "scale=4; 3.14159 * $radius * $radius" | bc)
echo "Area: $area" # Area: 78.5397
# bc with a here-string (cleaner)
result=$(bc <<< "scale=3; 10 / 3")
echo "$result" # 3.333Practical: Generate a Counter Loop
#!/usr/bin/env bash
# Count files in a directory and report
dir="${1:-.}" # default: current directory
count=0
for f in "$dir"/*; do
[[ -f "$f" ]] && (( count++ ))
done
echo "Found $count files in $dir"
# Show percentage if target is 100
target=100
pct=$(( count * 100 / target ))
echo "That's $pct% of target"What does `echo $(( 7 / 2 ))` print?
Write a script that takes two numbers as arguments and prints:
- Their sum
- Their difference (first minus second)
- Their product
- Their integer quotient
- Their remainder (modulo)