0

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:
dwitvliet
  • 7,242
  • 7
  • 36
  • 62
Suren
  • 37
  • 7

1 Answers1

1

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`
konsolebox
  • 72,135
  • 12
  • 99
  • 105