Shell – Redirect and pipe output

io-redirectionpipeshell

rpm -qa > file | wc -l returns 0. What am I doing wrong? I think it is clear what my goal is, but it isn't working. Is this a job for xargs or tee?

Best Answer

Yes, this is job for tee:

rpm -qa | tee file | wc -l

In this construction a | b a's stdout goes to stdin of b. In case of a > file | b all output form a goes to file and nothing goes to b stdin. tee command make a copy of all it receives on stdin to both file and stdout.

Related Question