I have the following entry in a bash script:

 echo "Please see attached file" | mailx -S smtp=$smtpServer -s "Subject of the mail" -a $logfile -r "[email protected]" [email protected] [email protected]

It's working fine if there is no error in sending mail, but if there is an error, mailx will show the error message and wait for user to press the enter key. How can I want to avoid that? It should not wait for the user to press the enter key.

  • Note the mailx on my linux system uses -A for attachments and -a for headers. Commented Jul 14, 2017 at 13:13
  • Does sending into subshell and also into background help? (command1 | command2 &>/dev/null &) ; someOtherStuffHere
    – user80158
    Commented Jul 14, 2017 at 17:09
  • Did any of the answers solve your problem? If so, please use the checkmark to tell the system that the question is Answered. Thank you!
    – Jeff Schaller
    Commented Jul 30, 2017 at 12:13

3 Answers 3


You would normally try to use the application switches for non-interactive mode or suppressing errors for this. I didn't find any explicit ones for mailx after quick skim of the docs, however.

A more general approach of doing this would be to use the timeout command for this, which runs the specified command and kills it if it is still running after specified timeout.

For example, in your case, the command would be:

echo "Mail body" | timeout 15s mailx ${MAILX_OPTIONS}

This command will kill mailx if it's still running after 15 seconds, which should be only in the case of a lingering error message.


In the gross-hack category of solutions, send along a gratuitous carriage return, which will be there if needed, and thrown away if not:

{ echo "Please see attached file"; echo; } | mailx ...

You can run the command in an expect shell, which will watch allow you to look for prompts and send information in response.

expect "PROMPT"
send "RESPONSE\n";
  • 1
    This is a good idea in general, but how would the actual implementation look for this use case?
    – Jeff Schaller
    Commented Jul 14, 2017 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .