#!/bin/bash
MESSAGE="Line one. /n"
MESSAGE="$MESSAGE Line two. /n"
MESSAGE="$MESSAGE Line three."
echo $MESSAGE | mail -s "test" "[email protected]"
Is that how I should get each line, on its own line?
#!/bin/bash
MESSAGE="Line one. /n"
MESSAGE="$MESSAGE Line two. /n"
MESSAGE="$MESSAGE Line three."
echo $MESSAGE | mail -s "test" "[email protected]"
Is that how I should get each line, on its own line?
Use a heredoc.
mail -s "test" "[email protected]" << END_MAIL
Line one.
Line two.
Line three.
END_MAIL
END_MAIL) it will prevent parameter expansion, command substitution, etc., which may be desirable.Change:
echo $MESSAGE | mail -s "test" "[email protected]"
To:
echo -e $MESSAGE | mail -s "test" "[email protected]"
-e switch expands the \n newline characters as expected and is easier to use in a script on a single line than the heredoc method.The heredoc advice is good, plus you might want to consider using mailx for which there exists a Posix standard or perhaps sendmail which will exist if the mailer is either sendmail or postfix. (I'm not sure about qmail.)
Unless using sendmail, it's also a good idea to set the MAILRC variable to /dev/null to bypass the user's configuration script, if any.
sendmail in shell scripts. +1