Sending mail in bash script outputs literal \n instead of a new line
10,032
Solution 1
In bash you should use following syntax
message_txt=$'This is just a test.\n Goodbye!'
Single quotes preceded by a $
is a new syntax that allows to insert escape sequences in strings.
Check following documentation about the quotation mechanism of bash
for ANSI C-like escape sequences
Solution 2
You can also embed newlines directly in a string, without an escape sequence.
message_txt="This is just a test.
Goodbye!"
Author by
Grant
Just a music guy dabbling, largely stumbling, in the hallowed ground of the web's inner workings.
Updated on June 11, 2022Comments
-
Grant almost 2 years
I am using the following bash script to send an email
#!/bin/bash recipients="[email protected], [email protected]" subject="Just a Test" from="[email protected]" message_txt="This is just a test.\n Goodbye!" /usr/sbin/sendmail "$recipients" << EOF subject:$subject from:$from $message_txt EOF
But when the email arrives the $message_txt content is printed literally like this :
This is just a test.\n Goodbye!
Instead of interpreting the new line like this :
This is just a test. Goodbye!
I've tried using :
echo $message_txt echo -e $message_txt printf $message_txt
But the result is always the same. Where am I going wrong?
What am I doing wrong?
-
Grant over 8 yearsYou know I didn't even think to try that!