bash 邮件:主题中的命令输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12460448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:17:27  来源:igfitidea点击:

bash mail: command output in subject

bashemail

提问by JuanPablo

I try send a mail with mailcommand, with the output of other command in the subject

我尝试使用mail命令发送邮件,主题中包含其他命令的输出

subj="hello from $(hostname -s) $(date)" 
echo "data" | mail -s $subj mail@mail

but I only get the first part of subject (hello from).

但我只得到主题 ( hello from)的第一部分。

why?

为什么?

回答by dogbane

You need to quote your subject, like this:

你需要引用你的主题,像这样:

echo "data" | mail -s "$subj" mail@mail

If you don't quote it, the mailprogram will not know where your subject ends and will take the first "word" (hello) as the subject and everything else as an address.

如果您不引用它,mail程序将不知道您的主题在哪里结束,并将第一个“单词”(hello)作为主题,其他所有内容作为地址。

In general, it is good practice to always quote your variables.

通常,始终引用变量是一种很好的做法。

回答by Gestudio Cloud

/youScriptOrOutput.sh | mail -s "Subject from host $(hostname -s) $(date)" [email protected]

/youScriptOrOutput.sh | mail -s "主题来自主机 $(hostname -s) $(date)" [email protected]