Search code examples
bashcountlinesteewc

bash obtain wc -l number and display in one command?


I'm pretty sure this is going to be obvious, but currently im doing this:

count=`find $dir -type f \( -perm -007 \) -print 2>/dev/null | wc -l`

This gets me the number i want, but dosen't display anything on screen (although i chuck away error lines anyhow).

Is there a way to do this (obtain the wc -l count into count variable) while also displaying the output to the console, in one command ? I'm pretty sure something like tee could be used here, but my brain isn't working like it should.

Otherwise, i guess writing to a temp file and console using tee and cat it back into wc would work, but I'm pretty convinced there must be a more elegant way of doing this.

edit: Sorry, it seems the question was unclear. I don't want to show the count to screen, i want to show the output that i've been counting, i.e: the output from find


Solution

  • Ah, so you want to print the normal output, and have the number of matches in $count?

    Try this:

    count=`find $dir -type f \( -perm -007 \) -print 2>/dev/null | tee /dev/tty | wc -l`