Shell Command
Sum Integers
Programming
Coding Tutorial
Command Line Tools

Shell command to sum integers, one per line?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you have a file or pipeline where each line contains one integer, the simplest shell solution is usually awk. It is fast, readable, and works equally well with files and standard input.

The Best Default: awk

For a file named numbers.txt, use:

bash
awk '{ sum += $1 } END { print sum }' numbers.txt

If numbers.txt contains:

text
5
10
15

the output is:

text
30

This works because awk processes input line by line, adds the first field from each line into sum, and prints the final value at the end.

Summing From A Pipeline

The same pattern works when numbers come from another command.

bash
printf '5\n10\n15\n' | awk '{ sum += $1 } END { print sum }'

That is often more useful than the file example because shell data frequently arrives through pipes.

Handling Blank Lines Safely

If the input may contain empty lines, filter them out so the intent stays explicit.

bash
awk 'NF { sum += $1 } END { print sum }' numbers.txt

NF means "number of fields". Empty lines have zero fields, so they are ignored.

Pure Shell Loop

If you want a solution without awk, a shell loop also works.

bash
1sum=0
2while IFS= read -r line; do
3  [ -n "$line" ] || continue
4  sum=$((sum + line))
5done < numbers.txt
6printf '%s\n' "$sum"

This is fine for small inputs and easy to understand, but it is usually slower than awk on large files.

Summing A Specific Column

Sometimes the input has more than one field per line and you want only one column. awk makes that trivial.

bash
awk '{ sum += $2 } END { print sum }' table.txt

That adds the second whitespace-separated field from each line.

If the file is comma-separated instead of space-separated, set the field separator.

bash
awk -F',' '{ sum += $3 } END { print sum }' values.csv

Input Validation

If the input is supposed to be integers but might contain junk, guard against it explicitly.

bash
1awk '
2  /^[+-]?[0-9]+$/ { sum += $1; next }
3  { printf "invalid line: %s\n", $0 > "/dev/stderr" }
4  END { print sum }
5' numbers.txt

This version accepts only plain signed integers and reports bad lines to standard error.

Why paste And bc Are Usually Not The Best Answer

You may see solutions like:

bash
paste -sd+ numbers.txt | bc

This works for simple cases, but it is less robust and less clear when inputs contain blank lines, invalid text, or very large streams. awk is a better general-purpose answer because it already understands line-oriented data processing.

Performance Notes

For large files, awk is usually the best tradeoff between simplicity and speed. A shell loop performs arithmetic in the shell process and can be slower. The paste | bc pattern builds a single long expression, which is less attractive as input size grows.

If all you need is a running sum from line-oriented numeric input, awk is exactly the right tool.

Common Pitfalls

A common mistake is using a shell loop without quoting or without read -r, which can mis-handle backslashes or whitespace.

Another issue is assuming every line is valid numeric input. If the file may contain headers, blank lines, or comments, either filter them first or add validation.

Developers also sometimes write sum += line in plain shell syntax outside arithmetic expansion. In POSIX-style shells, arithmetic must happen inside $(( ... )) or an equivalent construct.

Finally, be careful with the field you are summing. awk uses $1, $2, and so on, so the wrong column number can produce a believable but incorrect total.

Summary

  • The best default command for summing one integer per line is awk '{ sum += $1 } END { print sum }'.
  • The same command works with both files and pipelines.
  • Use NF or a numeric pattern if you need to skip blank or invalid lines.
  • A shell loop works, but it is usually slower and more error-prone than awk.
  • 'awk also makes it easy to sum a specific column in structured text input.'

Course illustration
Course illustration

All Rights Reserved.