bashksh

How to sum up multi-line of output of numbers?


I have a multi-line of output from some shell scripts, one integer number per line. Like:

12
11
55
337
11
34

Problem is, how could I sum up these numbers with a shell command? I tried sum, but it doesn't do what is intended:

<some_shell_scripts> |sum
36373     2 

Any simple solution in ksh or bash?


Solution

  • Pipe that to awk:

    <some_shell_scripts> | awk '{sum+=$1} END {print sum}'