LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   add new line to bash script output (https://www.linuxquestions.org/questions/linux-newbie-8/add-new-line-to-bash-script-output-4175708568/)

aristosv 02-25-2022 07:33 AM

add new line to bash script output
 
I am trying to send a small report via email using this bash script

Code:

#!/bin/bash

read -r -d '' report << EOM
---------------------------------
Host Name: $HOSTNAME
---------------------------------
Date: $(date)
---------------------------------
CPU Usage: $cpuusage
---------------------------------
Disk Usage: $diskusage
---------------------------------
Memory Usage: $memoryusage
---------------------------------
Temperature: $temperature
---------------------------------
Uptime: $uptime
---------------------------------
Local IP: $localip
---------------------------------
Public IP: $publicip
---------------------------------
EOM

{ echo Subject: Report - $HOSTNAME ; echo $report ; } | ssmtp $emailaddress

But when I receive the email, it's all one line.

Code:

--------------------------------- Host Name: raspberrypi --------------------------------- Date: Fri Feb 25 13:29:57 GMT 2022 --------------------------------- CPU Usage: 3% --------------------------------- Disk Usage: 5% --------------------------------- Memory Usage: 5% --------------------------------- Temperature: 45C --------------------------------- Uptime: 4 days --------------------------------- Local IP: 192.168.1.212 --------------------------------- Public IP: 139.138.204.42 ---------------------------------
How can I fix this?

lvm_ 02-25-2022 07:50 AM

Add double quotes - echo "$report"

aristosv 02-25-2022 07:51 AM

thank you

boughtonp 02-25-2022 08:13 AM


 
That applies for all variables, not just report.

ShellCheck is a useful tool for checking things like this.


MadeInGermany 02-26-2022 07:17 AM

The shell often expands unquoted $variables.
Often means: in commands and lists.
echo is a command; put quotes!
If you are in doubt if quotes are needed then put quotes!

Within " " (double quotes) the shell evaluates+substitutes $-expressions, such as ${variable} or $(command) or $((arithmetic)), but does not do word splitting and filename generation.

In this case the word splitting lead to the replacenment of newlines by spaces.

For completeness:
Within ' ' (single quotes, ticks) everything is literal; the shell does no substitution or expansion.


All times are GMT -5. The time now is 07:17 PM.