Thursday, April 03, 2008

Quick Bash Pointer

While writing a Bash script today, I came across an interesting situation.

I had gathered some output into a variable and I wanted to email those results to myself.

Typically you just use the mail command, specify a subject, and add a to address. Then just pipe in a file which will be used for the body text of the email.

However, I didn't have results in a file. I had them in a variable in memory. So rather than create a temp file and use the traditional approach, a friend of mine showed me the following approach which seems cleaner.

mail -s "subject" "to address" <<eof
$RESULT
eof


This will send everything after '<<eof' up to the next 'eof' to the mail command via stdin. More information can be found here about the usage of eof.

Original suggestion for this approach was taken from the following link, in the comments section.

No comments: