Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this line

 ( cat bodyText.txt; uuencode backup.tar ) | mail -s "backup" myEmail@myserver.com

inside a bash script that will be fired by cron.

This line is almost perfect, but it hangs waiting for Ctrl-D to be typed, so the message and the attachement could be sent.

How do I include a Ctrl-D inside a bash script to make this line work?

share|improve this question
why you not simply use cat bodyText.txt | uuencode backup.tar| mail -s "backup" myEmail@myserver.com – Rahul Patil Jan 10 '13 at 12:29
because this will produce two emails, one containing the text and the other containing the tar. – SpaceDog Jan 10 '13 at 12:42
Have you tried ( cat bodyText.txt; uuencode backup.tar ; echo -e "\n.\n" ) | mail -s "backup" myEmail@myserver.com already? – ott-- Jan 10 '13 at 12:44
@DigitalRobot have you tried that ? because i am receiving only one mail – Rahul Patil Jan 10 '13 at 12:46
this \n\n method does not works. It is expecting a Control D. Sending \n will do nothing. – SpaceDog Jan 10 '13 at 12:48
up vote 4 down vote accepted
  • the uuencode is : uuencode name < sourcefile

  • maybe just need a final empty line : add ; echo -e "\n\n" ; before the closing parenthesis, and try again?-

So in your case:

( cat bodyText.txt; uuencode backup.tar < backup.tar ; echo -e "\n\n" ; ) | mail -s "backup" myEmail@myserver.com

Another way is using mutt which handles attachments better, and knows about mime types, etc:

mutt -s "the subject" -a backup.tar -- myEmail@myserver.com < bodyText.txt
share|improve this answer
the echo is probably unneeded... I can't try it here, so try with it and try without it, and see which works ^^ – Olivier Dulac Jan 10 '13 at 12:36
not working for me. Same problem. – SpaceDog Jan 10 '13 at 12:49
try : uuencode backup.tar backup.tar for the uuencode part – Olivier Dulac Jan 10 '13 at 13:23
you may have an alias ? on your comment on @goodluck answer below, it looks as if your "uuencode something" does uuencode the file "something" ? see type -all uuencode in bash and if it's an alias, try \uuencode backup.tar backup.tar (with "\" to bypass the alias) – Olivier Dulac Jan 10 '13 at 13:25
I updated my answer: try the new syntax. And try the mutt alternative too. – Olivier Dulac Jan 10 '13 at 13:34

As others have said, Control-D is how you tell the shell to send end of file. Processes reading from streams should get end of file when the stream closes.

But it appears you're trying to implement something that already exists in more complete form. You could have reasons for that of course, but if you're interested in existing solutions, mpack/munpack are command line programs that mime encode files and send via mail. You can read the manpage to see if it does what you want.

share|improve this answer

Use verbose mode (-v) to avoid ctrl+d

echo -e 'Mail body'| mail -v -s 'Mail Subject' my@email.com
share|improve this answer

Do you mean:

uuencode backup.tar < bodyText.txt | mail -s "backup" myEmail@myserver.com
share|improve this answer
not working for me. The attachment arrives without the body text.... – SpaceDog Jan 10 '13 at 12:52
Your code to uuencode doesn't make any sense, and changing that part wouldn't help with the problem with mail in any case. You should write answers that explain the problem, not just dump a line of code: that would be more useful to readers and would help you figure out whether what you propose has a chance of solving the problem. – Gilles Jan 10 '13 at 13:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.