I need to count the number of lines of a given variable. For example I need to find how many lines VAR
has, where VAR=$(git log -n 10 --format="%s")
.
I tried with echo "$VAR" | wc -l)
, which indeed works, but if VAR
is empty, is prints 1
, which is wrong. Is there a workaround for this? Something better than using an if
clause to check whether the variable is empty...(maybe add a line and subtract 1 from the returned value?).
git log
produces either nothing, either some lines. So I need to treat the case of an empty var, because there won't be blank lines if the variable is not empty. – Libeleegit log -n 10
do generate empty lines. but I get what you want. – Capillarygit log -n 10 --format="%s"
. – Libelee