I'd like to run a find
command and then count the lines of the output as well as give out the output of the result. My straight-forward approach was this:
output=$(find ...)
lines=$(echo "$output" | wc -l)
echo "$output"
But unfortunately using echo
to pipe this into wc
adds a newline, so even if the find
did not find anything (zero lines of output), that wc
is giving out 1
(for the newline which the echo
appended.
I changed the echo
to a printf
to prevent appending the newline to the output but then also a one-line output of find
like var/
was without a newline, and hence the wc
gave out 0
.
The problem is in the capturing of the output ($(...)
). It strips trailing newlines which in my case are relevant.
Can this be prevented somehow?
Is there a completely different approach on my original task?