The following command e-mails myself a file:

sudo mail -a FILE_ADDRESS -s "Subject of E-mail" myemail@gmail.com

Question: How do I turn this command into a one-liner, so that I could just type something like

email_myself FILE_ADDRESS

A related question is as follows: is there a way for me to make it so that all I have to do is just right-click on a file, and then I can execute this command on it? Or do something like e-mail myself the contents of my clipboard (such that if it's a file, it e-mails me the file, or if it's text, it e-mails me the text)? (Note I'm using Ubuntu). I think this would be an incredibly useful script :)


Make a function:

email_myself() { sudo mail -a "$1" -s "Subject of E-mail" myemail@gmail.com; }

If you want your function to hang around permanently, and assuming that bash is your shell, add the definition as a line to your ~/.bashrc.

For those who prefer shell scripts to functions, create a file named email_myself, make it executable (chmod +x email_myself), and save it somewhere on your PATH:

sudo mail -a "$1" -s "Subject of E-mail" myemail@gmail.com

Other mail programs

On my system, the mail command does not work like that. For one, sudo is unnecessary. For another, -a does not attach a file; it is used to specify additional headers. Thus, I would use:

email_myself() { mail -s "Subject of E-mail" myemail@gmail.com <"$1"; }
  • I add a bin/ directory to my home directory and place shell scripts and other tools there (adding ~/bin/ to path). But a shell function works... – ChuckCottrill Oct 9 '14 at 21:19
  • 2
    @ChuckCottrill My ~/bin directory has over 500 scripts in it. I have found, though, that there are many who prefer functions. – John1024 Oct 9 '14 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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