LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2022, 07:33 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
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?
 
Old 02-25-2022, 07:50 AM   #2
lvm_
Member
 
Registered: Jul 2020
Posts: 926

Rep: Reputation: 337Reputation: 337Reputation: 337Reputation: 337
Add double quotes - echo "$report"
 
1 members found this post helpful.
Old 02-25-2022, 07:51 AM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
thank you
 
Old 02-25-2022, 08:13 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

That applies for all variables, not just report.

ShellCheck is a useful tool for checking things like this.

 
Old 02-26-2022, 07:17 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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.

Last edited by MadeInGermany; 02-26-2022 at 07:26 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to add new line to mp3 playlist bash script punkska1977 Linux - Newbie 2 02-14-2012 07:39 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
Bash: add date to output line blizunt7 Programming 7 11-10-2004 03:45 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration