๐Ÿ”„ 3. Bash Control Flow
Estimated reading: 3 minutes 50 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐ŸŸข Bash: Arithmetic (let, (( )), expr)

Or Copy Link

CONTENTS
Scroll to Top