Search code examples
linuxbashshellquote

How to format linux mpstat output in multiple lines


I have a small script where I appended the output of linux mpstat to a log file.

#/bin/bash
CPU_USAGE=$(mpstat)
echo $CPU_USAGE >> temp.log

The problem is that the output of mpstat on the terminal is formatted properly in 3 lines like so

enter image description here

However, the output to the file is all in one line.

enter image description here

How do I format the output like the one on the terminal?


Solution

  • Just quote the variable so it is not seen as several different parameters to be printed one after the other:

    echo "$CPU_USAGE" >> temp.log