Shell by Example: Arithmetic POSIX + Bash

Shell scripting supports arithmetic operations through several mechanisms. This example covers the main ways to do math in shell scripts.

POSIX arithmetic expansion with $(( )):

Edit
#!/bin/sh
echo "Basic arithmetic with \$(( )):"
echo "  5 + 3 = $((5 + 3))"
echo "  10 - 4 = $((10 - 4))"
echo "  6 * 7 = $((6 * 7))"
echo "  20 / 4 = $((20 / 4))"
echo "  17 % 5 = $((17 % 5))" # Modulo (remainder)
Output:
Basic arithmetic with $(( )):
  5 + 3 = 8
  10 - 4 = 6
  6 * 7 = 42
  20 / 4 = 5
  17 % 5 = 2

Variables in arithmetic (no $ needed inside):

Edit
#!/bin/sh
x=10
y=3
echo "With variables (x=$x, y=$y):"
echo "  x + y = $((x + y))"
echo "  x * y = $((x * y))"
echo "  x / y = $((x / y))" # Integer division
echo "  x % y = $((x % y))"
Output:
With variables (x=10, y=3):
  x + y = 13
  x * y = 30
  x / y = 3
  x % y = 1

Compound assignment:

Edit
#!/bin/sh
count=0
count=$((count + 1))
echo "After increment: count = $count"
Output:
After increment: count = 1

Bash

Bash provides shorter increment/decrement:

Edit
#!/bin/bash
((count++))
((count--))
((count += 5))
((count *= 2))

Comparison operators (return 1 for true, 0 for false):

Edit
#!/bin/sh
a=5
b=10
echo "Comparisons (a=$a, b=$b):"
echo "  a < b: $((a < b))"
echo "  a > b: $((a > b))"
echo "  a == b: $((a == b))"
echo "  a != b: $((a != b))"
echo "  a <= b: $((a <= b))"
echo "  a >= b: $((a >= b))"
Output:
Comparisons (a=5, b=10):
  a < b: 1
  a > b: 0
  a == b: 0
  a != b: 1
  a <= b: 1
  a >= b: 0

Logical operators:

Edit
#!/bin/sh
echo "Logical operators:"
echo "  1 && 1 = $((1 && 1))"
echo "  1 && 0 = $((1 && 0))"
echo "  1 || 0 = $((1 || 0))"
echo "  !0 = $((!0))"
echo "  !1 = $((!1))"
Output:
Logical operators:
  1 && 1 = 1
  1 && 0 = 0
  1 || 0 = 1
  !0 = 1
  !1 = 0

Bitwise operators:

Edit
#!/bin/sh
echo "Bitwise operators:"
echo "  5 & 3 = $((5 & 3))"     # AND
echo "  5 | 3 = $((5 | 3))"     # OR
echo "  5 ^ 3 = $((5 ^ 3))"     # XOR
echo "  ~5 = $((~5))"           # NOT
echo "  4 << 2 = $((4 << 2))"   # Left shift
echo "  16 >> 2 = $((16 >> 2))" # Right shift
Output:
Bitwise operators:
  5 & 3 = 1
  5 | 3 = 7
  5 ^ 3 = 6
  ~5 = -6
  4 << 2 = 16
  16 >> 2 = 4

Ternary operator is a shorthand way to write an if-else statement.

The syntax is: $((condition ? expression1 : expression2))

If the condition is true, the expression1 is evaluated and returned. Otherwise, the expression2 is evaluated and returned.

This example shows how to use the ternary operator to find the maximum of two numbers.

Edit
#!/bin/sh
a=10
b=20
max=$((a > b ? a : b))
echo "Max of $a and $b: $max"
Output:
Max of 10 and 20: 20

Parentheses for grouping:

Edit
#!/bin/sh
echo "Order of operations:"
echo "  2 + 3 * 4 = $((2 + 3 * 4))"
echo "  (2 + 3) * 4 = $(((2 + 3) * 4))"
Output:
Order of operations:
  2 + 3 * 4 = 14
  (2 + 3) * 4 = 20

Bash

Different bases:

Edit
#!/bin/bash
echo "Different bases:"
echo "  Hex 0xFF = $((0xFF))"        # works in sh and bash
echo "  Octal 077 = $((077))"        # works in sh and bash
echo "  Binary 2#1010 = $((2#1010))" # bash-specific
Output:
Different bases:
  Hex 0xFF = 255
  Octal 077 = 63
  Binary 2#1010 = 10

Using expr (older method, POSIX):

Edit
#!/bin/sh
echo "Using expr:"
echo "  5 + 3 = $(expr 5 + 3)"
echo "  10 - 4 = $(expr 10 - 4)"
echo "  6 \* 7 = $(expr 6 \* 7)" # Note: * must be escaped
echo "  20 / 4 = $(expr 20 / 4)"
Output:
Using expr:
  5 + 3 = 8
  10 - 4 = 6
  6 \* 7 = 42
  20 / 4 = 5

expr for string length and matching:

Edit
#!/bin/sh
str="hello"
echo "  Length of '$str': $(expr length "$str")"
Output:
  Length of 'hello': 5

Floating-point arithmetic requires external tools:

Edit
#!/bin/sh
echo "Floating-point with bc:"
echo "  5.5 + 3.2 = $(echo "5.5 + 3.2" | bc)"
echo "  10 / 3 = $(echo "scale=2; 10 / 3" | bc)"
echo "  sqrt(2) = $(echo "scale=4; sqrt(2)" | bc)"
Output:
Floating-point with bc:
  5.5 + 3.2 = 8.7
  10 / 3 = 3.33
  sqrt(2) = 1.4142

Bash

Bash’s let command can be used to perform arithmetic operations.

Edit
#!/bin/bash
let "result = 5 + 3"
echo "let result: $result"

x=5
let "x++"
echo "let x: $x"

let "y = x * 2"
echo "let y: $y"
Output:
let result: 8
let x: 6
let y: 12

Bash

Bash’s $(( )) can be used for arithmetic in conditions.

Edit
#!/bin/bash
x=10
if ((x > 5)); then
    echo "x is greater than 5"
fi
Output:
x is greater than 5

Bash

Declare integer variables with declare -i to automatically evaluate arithmetic expressions.

Edit
#!/bin/bash
declare -i num
num="5 + 3" # Automatically evaluated
echo "Declared int: $num"
Output:
Declared int: 8

« Temporary Files | String Manipulation »