Count number of lines of output from previous program

You can use tee to split the output stream sending one copy to wc and the other copy to STDOUT like normal.

program | tee >(wc -l)

The >(cmd) is bash syntax which means run cmd and replace the >(cmd) bit with the path to (a named pipe connected to) that program's STDIN.


One option is to use awk, which can do the counting and print to stdout.

program | awk '{ print } END { print NR }'

In awk, NR is the current line number. You can accomplish the same with perl:

program | perl -pe 'END {print "$.\n"}'

Or sed:

program | sed -n 'p;$='

my favorite option:

program | grep "" -c