Because the page
variable can contain arbitrary text, you shouldn't use echo
, you should use printf
.
times="$(printf %s\\n "$page" | wc -l)"
Also note that if you have more than one trailing newline in the output of your grep
command, you won't get the expected result no matter what, because command substitution strips trailing newlines and the echo
command or the printf
command I use alike will add in exactly one trailing newline, regardless of how many were stripped from the grep
output when the $page
variable was being set.
Of course, even in this case the $times
variable will contain a bunch of extra spaces at the start, because that's how wc -l
will output its information. So this answer doesn't address the broader issue with your script: It looks like you are pressing bash
into service where awk
would serve better.
I could be wrong about that, depending on what you intend to do with the $times
variable, but I seriously doubt it. Most likely you can do what you need (all of what you need) with an awk
one-liner.