On Wed, Dec 21, 2016 at 05:00:19PM +0100, Stéphane MERLE wrote: > lemail=$(cat)
Instead of buffering the message into a shell variable, buffer it into a temporary file (and set a "trap" command to delete the file). You can then inspect the file content before sending the right message. It is possible to pre-unlink the file, while keeping an open handle: msg=$(mktemp /tmp/msg.XXXXXX) || exit 75 cat > $msg || { rm $msg; exit 75; } exec 3< $msg || { rm $msg; exit 75; } rm $msg You can then read the file multiple times by rewinding file descriptor 3: perl -e 'open IN, "<&", 3; seek(IN, 0, 0);' if grep 'pattern' <& 3 >/dev/null; then set -- "$@" "$additional_recipient" fi And finally perl -e 'open IN, "<&", 3; seek(IN, 0, 0);' exec sendmail -i "$@" <&3 -- Viktor.