Arithmetic Operations and Numerical Computation Techniques in Shell Scripting

Arithmetic Operators and Common Commands

Common Arithmetic Operators

Operator Description
+, - Addition and subtraction
*, /, % Multiplication, division, and modulo
** Exponentiation
++, -- Increment and decrement
!, &&, `
<, <=, >, >= Comparison operators
==, !=, = Equality and inequality checks
<<, >> Bitwise left and right shift
~, ` , &, ^`
=, +=, -=, *=, /=, %= Assignment operators

Common Arithmetic Commands

Command Description
(()) Efficient integer arithmetic operator
let Integer arithmetic, similar to (())
expr Integer arithmetic with additional string functions
bc Calculator supporting integers and decimals
$[ ] Integer arithmetic syntax
awk Supports both integer and decimal calculations
declare -i Declares integer variables for arithmetic

Practical Computation Examples

Using Double Parentheses

Expression Behavior
((x=x++)) Assign first, then increment
((x=++x)) Increment first, then assign
x=$((x+1)) Compute and assign result to x
((a>5 && b<3)) Comparison usable in conditionals
echo $((3+4)) Directly output computation result

Increment Behavior:

  • x=x++ outputs the original value, then increments x.
  • x=++x increments first, then outputs the updated value.

Tests:

x=6; echo $((x++)); echo $x
# Output:
# 6
# 7

x=6; echo $((++x)); echo $x
# Output:
# 7
# 7

Alternative Calculation Commands

let Command

let x=x+3  # Equivalent to ((x=x+3))

bc Calculator

Performs decimal arithmetic via pipelines.

echo "2.5 + 3.5" | bc
val=8; val=$(echo "$val * 2" | bc)

awk Arithmetic

Handles integers and decimals.

echo "3.2 4.1" | awk '{print $1+$2, $1*$2}'
# Output: 7.3 13.12

$[ ] Syntax

echo $[5+3] $[5*3] $[5**2]
# Output: 8 15 25

Use Cases

Summation from 1 to 10

Method 1:

echo $(seq -s "+" 10)=$(seq -s + 10 | bc)
# Output: 1+2+3+4+5+6+7+8+9+10=55

Method 2:

echo $(echo {1..10} | tr " " "+")=$(echo {1..10} | tr " " "+" | bc)

Method 3 (expr):

echo $(seq -s + 10)=$(seq -s " + " 10 | xargs expr)

Method 4 ((())):

echo $(seq -s + 10)=$(( $(seq -s + 10) ))

Reading Input with read

Basic Usage

read -t 10 -p "Enter two numbers: " n1 n2

Calculation Script

#!/bin/bash
read -p "First number: " n1
read -p "Second number: " n2

echo "Sum: $((n1+n2))"
echo "Difference: $((n1-n2))"
echo "Product: $((n1*n2))"
echo "Quotient: $((n1/n2))"
echo "Power: $((n1**n2))"

Adding Integer Validation

#!/bin/bash

read -p "First number: " val1
expr "$val1" + 0 &>/dev/null
if [ $? -ne 0 ]; then
    echo "Please enter an integer."
    exit 1
fi

read -p "Second number: " val2
expr "$val2" + 0 &>/dev/null
if [ $? -ne 0 ]; then
    echo "Please enter an integer."
    exit 2
fi

echo "Sum: $((val1+val2))"
echo "Difference: $((val1-val2))"
echo "Product: $((val1*val2))"
echo "Quotient: $((val1/val2))"
echo "Power: $((val1**val2))"

Tags: Shell Scripting Arithmetic Operations Linux Commands Bash Scripting numerical computation

Posted on Thu, 24 Sep 2026 16:06:44 +0000 by JamieinNH