5

I am using the following bash script to send an email

 #!/bin/bash

 recipients="me@web.com, you@web.com"
 subject="Just a Test"
 from="test@test.com"

 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
  • 1,297
  • 2
  • 16
  • 39

2 Answers2

5

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

deimus
  • 9,565
  • 12
  • 63
  • 107
2

You can also embed newlines directly in a string, without an escape sequence.

message_txt="This is just a test.
Goodbye!"
chepner
  • 497,756
  • 71
  • 530
  • 681