twooBeers
twooBeers

Reputation: 196

redirection bash: content from variable, not from file

i am using mutt to create and send mails from a bash script.

like this:

$ mutt -s SUBJECT MAIL_TO_ADDRESS < BODY_CONTENT_FILE

So i always have to write a file before calling mutt, is there a solution to pass information directly from script or shell variable ?

Upvotes: 1

Views: 1025

Answers (3)

Romain
Romain

Reputation: 1343

Yes you can.

The expression < BODY_CONTENT_FILE at the end of a line is the same as cat BODY_CONTENT_FILE | at the beginning of the line.

So you can use the following snippet if your content is in a variable:

echo $VARIABLE | mutt -s SUBJECT MAIL_TO_ADDRESS

You can replace echo $VARIABLE by whatever you want which its output is on stdout.

Upvotes: 1

Mark Reed
Mark Reed

Reputation: 95267

Sure.

For literal content, you can use a here-document:

mutt -s "$SUBJECT" "$ADDRESS" <<EOF
body of message goes here
and all of it is included
until you have a line with the terminator
which is whatever you put after the `<<`
in this case,
EOF

Note that parameters will be expanded inside the here-document as if it were a double-quoted string, unless you quote the terminator (with e.g. <<"EOF"), in which case it will be treated as a a single-quoted string instead.

If it's a short message, or already in a variable, it's probably easier to use a here-string instead:

mutt -s "$SUBJECT" "$ADDRESS" <<<"$BODY"

Upvotes: 1

chepner
chepner

Reputation: 531325

Use a here document

$ mutt -s SUBJECT ADDRESS <<EOF
long
multiline
message
EOF

or, if the message is short, bash allows a here string:

$ mutt -s SUBJECT ADDRESS <<< "short message"

Either can contain a parameter expansion if you already have the text in a variable.

Upvotes: 2

Related Questions