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

2024-03-24

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

 #!/bin/bash

 recipients="[email protected] /cdn-cgi/l/email-protection, [email protected] /cdn-cgi/l/email-protection"
 subject="Just a Test"
 from="[email protected] /cdn-cgi/l/email-protection"

 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!'

单引号前面带有 a$是一种新语法,允许在字符串中插入转义序列。

检查以下内容文档 http://wiki.bash-hackers.org/syntax/quoting关于报价机制bash用于类似 ANSI C 的转义序列

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 bash 脚本中发送邮件输出文字 \n 而不是换行 的相关文章

随机推荐