bash - count and output lines from command
<p>I'm writing a small script that needs to run a program that outputs multiple lines, and then display the count of those lines. The program, however, can take several seconds to run and I would rather not run it twice, once for the output and another for the count.</p>

<p>I can do it running the program twice:</p>

<pre><code>#!/bin/bash
count=$(program-command | wc -l)
program-command
printf "$count lines"
</code></pre>

<p>Is there a way to get the count and output while only running the program once? This output has formatting, so ideally that formatting (colors) would be preserved.</p>