How to use the Unix command-line mail tool

John Kerl
kerl at emtek dot com
April 28, 1997


How do I use mail? How do I save a message as a file? How do I forward a message? How the heck do I go back up a line when I've made a mistake, without having to start over from scratch??


There are two command sets in mail: one is used at the mail prompt, and one is used while composing messages. They are both generally just a single letter (though the latter are preceded by a ~ at the beginning of a line — otherwise everything you typed would be a command, not part of the message body!).

I'll discuss both command sets, as well as a couple of items for your ~/.mailrc, which sets up some preferences. Then I'll briefly mention aliases and a few command-line arguments (though your manpage on mail will have full details).

There is more than one mail tool out there; I'll be referring to /usr/ucb/Mail on Sun, and /usr/bin/mail on IBM (which function identically, as far as I can tell). These are the default mail tools for sa2000, in the Emtek product, and in the engineering network within Emtek as well. These are probably the ones you're already using (do 'which mail' at the csh if you're not sure).

You'll see that there are very simple ways (just a few keystrokes) to edit messages, read in other messages or files on your system, and to save messages as files. Using these commands, your mailtool will be more integrated with the rest of your work, and will become more of a joy to use.


Commands at the mail prompt

When you type 'mail' at the csh, you will see something like:

  sa2000<clinical>@ds35g [20]% mail
  Mail version SMI 4.1-OWV3 Mon Sep 23 07:17:24 PDT 1991  Type ? for help.
  "/usr/spool/mail/sa2000": 3 messages 1 new
      1 root       Wed Aug  9 18:58   13/316   Returned mail: user unknown
   U  2 fred       Wed Aug  9 18:59   13/319   I'll be out sick today
  >   3 sa2000     Wed Aug  9 19:00   12/307   Reminder

The N means new; U means not new but still not read (i.e. you quit mail tool without reading that message the last time you read your mail).

The > points to the current message.

The & is the mail prompt. You can change this as shown below, but the default (until you do change it) is &.

The most basic commands are h, z and z-; d and u; x and q; m and r; and s. I'll describe each. First, though, several of these basic commands operate on one or more message numbers. Here are the ways you specify message numbers — the numbers themselves, or some shortcut characters:

n Message number n, such as 3.
. The current message (marked with '> ').
^ The first undeleted message.
$ The last message.
+ The next undeleted message.
- The previous undeleted message.
* All messages.
n-m An inclusive range of message numbers.
(username) All messages from (username).
/(string) All messages with (string) in the subject line (case ignored).

Now that you know how to specify which message(s) you want a command to operate on, here are explanations of those basic commands:

(message number or shortcut) To view a message, just type its number; to view the last message, just type $; etc. A carriage return on an empty line means 'view the next message' — it's equivalent to n (or +).
h Shows you a screenful of message headers (a "header" being the number, sender, date, size and subject).

h with no message number shows the current screenful of messages (the number that make up a screenful is set with the screen variable, described below).

h$ shows you the last screenful of messages — which is usually what you're interested in (this is usually the first thing I type when I start mail).

h1 or h^ shows you the first screenful of messages.

z or z- If there is more than a screenful of messages, then z will show the next screenful, and z- will show the previous screenful.
d Mark message(s) for deletion.

d with no number marks the current message for deletion.

d with a number (or +, -, $, etc.) will mark the specified message(s) for deletion. To delete messages 1 to 3, you could do d 1-3, or d 1 2 3 (or d *, in this example where there are only 3 messages).

I said 'mark for deletion' instead of 'delete' because the changes you make are only saved when you type q.

u is 'undelete'. This just unmarks the message for deletion. You can do u followed by a number to undelete a specific message. But if you delete a message, and then quit with a q without having undeleted it, then that message really is gone.
q or x q quits and saves your changes; x quits without saving your changes. If you quit with an x, then any messages you d'ed will not actually be deleted the next time you invoke mail — also the little N for unread will still be there even if you read the message, etc. It's as if you were never there. This is sometimes a quick and dirty way to recover if you've deleted the wrong message, etc. — or if you aren't sure what you've done and just want to bail out.
m Start composing a message to a user or users, e.g. 'm fred terry sa2000'. Of course you can also send messages from the command line, rather than the mail prompt, using 'mail fred terry sa2000'.
r is used to reply to message — e.g. if you want to tell Fred 'Get well soon' in my example above, then you could do r 2.
s is used to save a message to a file. For example, if you want to save the 'Reminder' message above as 'reminder' in your current directory, you could use 's reminder'. Or 's /tmp/foo'> to save as /tmp/foo, etc.

If the specified file already exists, the message you save will be appended. So, this makes a mail folder. I find folders invaluable. I have a directory called /home/kerl/mail; in it are about a dozen folders. This helps me organize things that I want to save, but that I don't want to have to see cluttering up my in-box everytime I read my mail. I can read a folder with mail -f (folder name) at the csh prompt, or f (folder name) at the mail prompt.

A common sequence of keystrokes for me is: Read a message; type 's /home/kerl/mail/eng' (or whatever folder); then 'd.'. There is no command to *move* a message to a folder, as there is in the graphical mailtool. Rather, you copy a message to a folder, then delete from you in-box, as two separate steps.

! can be used to get a shell — but ^Z is just as useful, if not moreso, since ^Z gets you back to the shell you invoked mail from, not a new one.
= just prints the current message number.


prints a summary of commands that you can use at the mail prompt. This is a handy reminder.
v Puts the incoming message into vi; this may be a nicer way to read your mail than the PAGER (see below).

Commands while composing a message

The commands used while composing a message (say you used m or r to get a message going) begin with a ~ at the beginning of a line. This is so that q doesn't always mean quit, etc.

The most useful ones are as follows:

~m <numlist> Append the contents of message numbers <numlist> indented by the value of the indentprefix variable (see below), or TAB by default. This is nice when you're replying to someone, so that they know which message of theirs you're replying *to* — their own words are right there.

A common scenario for me is, I read a message and I want to reply to it. So, I read the message. Then, I type r to reply; then ~m to read in the message I'm replying to; then ~v (see below) which throws me into vi. When I'm done vi'ing, I type :wq in vi, then finally . to send the message.

~f <numlist>

Like ~m, but don't indent.


Display the contents of the message so far. Just shows it to you — doesn't affect the body of the message that goes out.

~r <filename>

Read in the contents of <filename>

~s <subject>

Set the Subject: line to the string specified by <subject> Comes in handy if you forgot, or changed your mind.

~t <addrlist>

Set user addresses in <addrlist> to the To: list.


Set the lists for To:, Subject:, Cc: and Bcc:, all at once.


Edit the message using an external editor (default is vi). This is great when you're sending anything of any length at all — it lets you move around. When you quit out of vi, you can continue doing other things to the message (i.e. type some more, or use more ~ commands), or finish off the message with a . .

Once you're in vi, you can use :r filename to include a file — perhaps a letter that you composed in vi earlier, some data or a configuration file you're sending someone, etc.

~w <filename>

Write the message to <filename>. Great for keeping a personal copy of an outgoing message for reference.


Quit, do not save letter. You can also use control-C twice.


End of input. Like . or control-D.


Print a list of ~ commands.


Quote a single tilde. Note that if you are rsh'ed to a host, rsh will 'eat' the first tilde, so you may need three of them.


Any of the commands below (setting and unsetting variables) can be done at the mail prompt, lasting until that mail tool is quit, or you can place them in your ~/.mailrc, which is read when the mail tool starts up, so that the setting will always be in place (until you change it at the mail prompt).


The alias command is useful when you constantly find yourself mailing to a group of people, or when someone has a long address.

Note that there is an NIS aliases file, which applies to everyone on your system. For example, at Emtek there are 'sunusers' and 'ibmusers' aliases that we use to mail customers in bulk.

But you can also define your own aliases. For example, if your fellow system administrators are Fred, Terry and Carol, you could put in your .mailrc

  alias sa fred terry carol

and then you can just send mail to 'sa'.

My sister has a nasty e-mail address — ''. I got tired of typing this so I set up the alias

  alias steph

and now I can just mail her by her first name.

If you type 'alias' with no arguments at the mail prompt, you'll get a list of your current personal aliases.

Command-line arguments

-N Don't print the initial screenful of headers when the tool starts.

I always use this option, since I normally have several dozen messages in my in-box, and since the first few dozen usually are nothing new. I use the following sequence when reading my mail: Type 'mail -N' at the csh prompt; then 'h$' at the mail prompt to see the newest messages; then 'z-' as many times as necessary till I get to the first unread message.

-s Set the subject line when mailing to someone. Often useful if you have the message already written; e.g. 'mail -s "Meeting minutes" fred < ~sa2000/meeting-minutes'.
-f (folder name) Read mail messages from the specified folder file, rather than reading your system in-box. Useful when you've moved messages to a folder, perhaps with s as described above.