在bash脚本中发送邮件输出文字\ n而不是新行

时间:2015-09-25 10:06:04

标签: linux bash email sendmail

我使用以下bash脚本发送电子邮件

 #!/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

但是当电子邮件到达时,$ message_txt内容的打印字面意思如下:

 This is just a test.\n Goodbye!

而不是像这样解释新行:

 This is just a test.
 Goodbye!

我尝试过使用:

 echo $message_txt
 echo -e $message_txt
 printf $message_txt

但结果总是一样的。我哪里错了?

我做错了什么?

2 个答案:

答案 0 :(得分:3)

在bash中,您应该使用以下语法

message_txt=$'This is just a test.\n Goodbye!'

$开头的单引号是一种新语法,允许在字符串中插入转义序列。

检查documentation关于ANSI {-like转义序列的bash的引用机制

答案 1 :(得分:1)

您也可以直接在字符串中嵌入换行符,而不使用转义序列。

message_txt="This is just a test.
Goodbye!"