Skip to main content

How to Calculate Average in Bash

• 2 min read
bash math data processing statistics

Quick Answer: Calculate Average of Numbers

Use awk for the fastest method: awk '{sum += $1; count++} END {print sum/count}' numbers.txt. For CSV files with a specific column: awk -F',' '{sum += $2; count++} END {print sum/count}' data.csv. For floating-point precision, use printf "%.2f\n" instead of print.

Quick Comparison: Averaging Methods

MethodSpeedPrecisionBest For
awkFastestAnyFiles, pipelines
bcMediumHighDecimal math, formatting
Bash loopSlowInteger onlySmall data, simple scripts
Parameter expansionVery fastInteger onlySimple arrays

Bottom line: Use awk for files and streams. Use bc when you need decimal precision. Use loops for small in-memory data.


Calculate average of numbers in Bash using various methods, from simple loops to powerful awk commands.

Method 1: Using awk

awk is the fastest and most efficient for calculating averages from files or streams. It automatically processes line-by-line without loading the entire file into memory.

# Average of column
awk '{sum += $1; count++} END {print sum/count}' numbers.txt

# Average with formatting (2 decimal places)
awk '{sum += $1; count++} END {printf "%.2f\n", sum/count}' numbers.txt

The END block runs after all lines are processed, printing the final average. Use this method for large files or when piping data.

Method 2: Using bc (For Decimal Precision)

Use bc when you need exact decimal arithmetic rather than rounded results. This approach combines awk for summing with bc for precise division.

# Sum and calculate average with decimal precision
total=$(awk '{sum += $1} END {print sum}' data.txt)
count=$(wc -l < data.txt)
echo "scale=2; $total / $count" | bc

The scale=2 tells bc to show 2 decimal places. Use this when financial calculations, measurements, or other precise values matter.

Method 3: Using a Bash Loop

When you need to do additional processing for each number (validation, filtering, logging), a while loop gives you full control. This method is slower than awk but offers flexibility.

sum=0
count=0

while read -r number; do
  ((sum += number))
  ((count++))
done < numbers.txt

average=$((sum / count))
echo "Average: $average"

This calculates integer averages. For decimal results, use bc after the loop instead of pure Bash arithmetic.

When to Use Each Method

Use awk when:

  • Working with files or pipelines
  • Processing large datasets
  • Performance matters
  • You need simple formatting

Use bc when:

  • Decimal precision is critical
  • Working with financial or scientific data
  • You need advanced math functions

Use loops when:

  • Small amounts of data in memory
  • You need to validate or filter before averaging
  • Simple scripts where clarity matters more than speed

From CSV Column

Calculate the average of a specific column in a CSV file using awk’s field separator option.

# Average of column 2
awk -F',' '{sum += $2; count++} END {printf "%.2f\n", sum/count}' data.csv

The -F',' tells awk to split on commas instead of whitespace.

With Multiple Statistics

awk '{
  sum += $1
  count++
  if (count==1) min=$1
  if ($1 < min) min=$1
  if ($1 > max) max=$1
}
END {
  printf "Sum: %d\n", sum
  printf "Count: %d\n", count
  printf "Average: %.2f\n", sum/count
  printf "Min: %d\n", min
  printf "Max: %d\n", max
}' numbers.txt

Practical Example

#!/bin/bash

file="$1"

if [ ! -f "$file" ]; then
  echo "File not found"
  exit 1
fi

# Calculate statistics
awk '{
  sum += $1
  count++
}
END {
  if (count > 0)
    printf "Total: %d numbers\n", count
    printf "Sum: %d\n", sum
    printf "Average: %.2f\n", sum/count
}' "$file"

Handling Decimal Numbers

# Use awk for floating point math
awk '{sum += $1} END {printf "%.2f\n", sum/NR}' prices.txt

The awk method is most efficient for calculating averages from large files.