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:
If numbers.txt contains:
the output is:
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.
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.
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.
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.
That adds the second whitespace-separated field from each line.
If the file is comma-separated instead of space-separated, set the field separator.
Input Validation
If the input is supposed to be integers but might contain junk, guard against it explicitly.
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:
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
NFor 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. - '
awkalso makes it easy to sum a specific column in structured text input.'

