在bash脚本中发送邮件会输出文字\n而不是换行 [英] Sending mail in bash script outputs literal \n instead of a new line
问题描述
我正在使用以下bash脚本发送电子邮件
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
但是当电子邮件到达时,$ message_txt内容将按如下方式打印:
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!
我尝试过使用:
echo $message_txt
echo -e $message_txt
printf $message_txt
但是结果总是一样的。我在哪里错了?
But the result is always the same. Where am I going wrong?
我在做什么错了?
推荐答案
在bash中,您应该使用以下语法
In bash you should use following syntax
message_txt = $'这只是一个测试。\再见!'
在单引号前加上 $
是一种新语法,允许在字符串中插入转义序列。
Single quotes preceded by a $
is a new syntax that allows to insert escape sequences in strings.
检查有关以下内容的文档 bash
对类似ANSI C的转义序列的引用机制
Check following documentation about the quotation mechanism of bash
for ANSI C-like escape sequences
这篇关于在bash脚本中发送邮件会输出文字\n而不是换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!