在 bash 脚本中发送邮件输出文字 \n 而不是新行
Sending mail in bash script outputs literal \n instead of a new line
我正在使用以下 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
但结果总是一样的。我哪里错了?
我做错了什么?
在bash中你应该使用下面的语法
message_txt=$'This is just a test.\n Goodbye!'
前面带有单引号的 $
是一种允许在字符串中插入转义序列的新语法。
检查以下 documentation 关于 bash
类 ANSI C 转义序列的引用机制
您也可以直接在字符串中嵌入换行符,无需转义序列。
message_txt="This is just a test.
Goodbye!"
我正在使用以下 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
但结果总是一样的。我哪里错了?
我做错了什么?
在bash中你应该使用下面的语法
message_txt=$'This is just a test.\n Goodbye!'
前面带有单引号的 $
是一种允许在字符串中插入转义序列的新语法。
检查以下 documentation 关于 bash
类 ANSI C 转义序列的引用机制
您也可以直接在字符串中嵌入换行符,无需转义序列。
message_txt="This is just a test.
Goodbye!"