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:

1 Answer 1

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`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.