πŸ”„ 3. Bash Control Flow
Estimated reading: 3 minutes 363 views

Bash Arithmetic – Using let, (( )), and expr for Math Operations


Introduction to Bash Arithmetic – Perform Math Using let, (( )), and expr

Although Bash is not a fully-fledged programming language, it supports basic arithmetic operations like addition, subtraction, multiplication, division, and modulus. These operations are commonly used in loops, counters, calculations, and conditional expressions.

In Bash, arithmetic can be performed using:

  • let – Built-in command
  • (( )) – Arithmetic evaluation expression
  • expr – External command (POSIX-compliant)

In this article, you’ll learn:

  • How to perform integer arithmetic in Bash
  • The differences between let, (( )), and expr
  • Syntax and use cases for each method
  • Best practices for readable and safe math operations

Supported Arithmetic Operators in Bash

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division (integer)
%Modulus (remainder)
**Exponentiation
++Increment
--Decrement

Method 1: Using let Command

The let command evaluates arithmetic expressions and assigns values to variables.

Example:

a=5
b=3
let sum=a+b
echo "Sum: $sum"

Output:

Sum: 8

No $ when assigning within let. Use $ only when echoing.


Increment/Decrement with let

let a++
let b--

Method 2: Using (( )) Expression

The (( )) syntax is cleaner and more modern, ideal for math and logic.

Example: Basic Math

a=10
b=4
(( result = a * b ))
echo "Product: $result"

Output:

Product: 40

Direct Echo from (( ))

echo $((20 + 5))   # Output: 25

Arithmetic in Conditionals

if (( a > b )); then
  echo "$a is greater than $b"
fi

Method 3: Using expr (POSIX Compatible)

expr is an older external utility used in older shell scripts.

Example:

a=7
b=2
sum=$(expr $a + $b)
echo "Sum: $sum"

Output:

Sum: 9

You must add spaces between operators and operands in expr. Use backticks or $(...) for capturing output.


Multiply with \*:

product=$(expr $a \* $b)

Asterisk must be escaped with \ to avoid shell globbing.


Comparison: let vs (( )) vs expr

Featurelet(( ))expr
Native to Bash Yes Yes External tool
Output to var Yes Yes Yes (with $() or “)
Works in if/loop Not directly Cleanly Verbose
Modern scripts Rarely used Preferred Outdated
Math syntaxOKBestLimited

Real-World Use Case – Loop Counter

for ((i=1; i<=5; i++)); do
  echo "Iteration: $i"
done

Output:

Iteration: 1  
Iteration: 2  
Iteration: 3  
Iteration: 4  
Iteration: 5

Summary – Bash Arithmetic

Bash arithmetic lets you integrate basic calculations directly into your scripts. Whether you use let, (( )), or expr, you can create logic-driven automations, counters, and more. For clarity and performance, prefer (( )) in modern Bash scripting.

Key Takeaways:

  • Use let for inline arithmetic (older style)
  • Use (( )) for modern, readable expressions
  • Use expr for legacy compatibility or POSIX scripts
  • Avoid floating-point math – Bash only supports integers

Real-world Uses:

  • Counting loop iterations
  • Calculating totals or differences
  • Creating numeric-based decision logic

FAQ – Bash Arithmetic


Can Bash handle floating-point arithmetic?
No. Bash natively supports only integer arithmetic. For floats, use tools like bc or awk.


Which method is best for math in Bash?
Use (( )) – it’s the cleanest, most modern, and easiest to read.


Why doesn’t expr work with *?
You must escape the asterisk:

expr 5 \* 2

How do I increment a variable in Bash?
Use:

(( count++ ))
# or
let count++

How do I subtract one number from another?
Use:

diff=$((a - b))
echo $diff

Share Now :
Share

🟒 Bash: Arithmetic (let, (( )), expr)

Or Copy Link

CONTENTS
Scroll to Top