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.
I can do it running the program twice:
#!/bin/bash
count=$(program-command | wc -l)
program-command
printf "$count lines"
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.
program-command > outputand thenwc -l output>as you suggested, thanks.