๐งฎ 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 expressionexprโ External command (POSIX-compliant)
๐ฏ In this article, youโll learn:
- How to perform integer arithmetic in Bash
- The differences between
let,(( )), andexpr - Syntax and use cases for each method
- Best practices for readable and safe math operations
๐ข Supported Arithmetic Operators in Bash
| Operator | Description |
|---|---|
+ | 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 withinlet. 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
| Feature | let | (( )) | 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 syntax | OK | Best | Limited |
๐ 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
letfor inline arithmetic (older style) - Use
(( ))for modern, readable expressions - Use
exprfor 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 :
