I want to capture the output of top into a variable instead of a file.
And I tried the below
top -n 1 -b | head > top_op
echo "inside: $top_op"
$top_op | grep Cpu > Cpu
echo "$Cpu"
But my output is just
Inside:
If you want to store output to variables, use command substitution:
top_op=$(top -n 1 -b | head)
echo "inside: $top_op"
Cpu=$(echo "$top_op" | grep Cpu)
echo "$Cpu"
Using backquotes is the but $()
is more recommended as it could be recursive without quoting.
top_op=`top -n 1 -b | head`