page=`grep $x /var/www/vhosts/example.com/statistics/logs/access_log | awk '{print $7}'| sort |uniq -c |sort -nr`times=`$page | wc -l`I am trying to save output of grep in a variable as page.But later I want to re-use that grep output to calculate some other thing.Below code throws:command not foundHow can I fix the issue and reuse the output?
Saving Output In a Variable and Re-using it
bash;shell script
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.