[Next Chapter] [Top page] [Previous Chapter]



The mail command enables the user to send and receive electronic mail messages to and from users on both the Unix system and remote users.

This is the basic mail command. Enhanced versions, such as programs that run under a windows program (e.g. mailtool), or screen-based versions of mail (e.g. elm) may be available, and you will probably find them preferable to mail. If so, much of the following can safely be ignored. Remember however that some version of mail will definitely be available on any unix system that you use.

Sending mail

To send a message to a user on your system, type:

% mail username

The cursor will move to the next line, and you will get a Subject: prompt. You can now type in the subject of your message, and then press <RETURN>. The cursor will go to the start of the next line and there will be no prompt. You now type in the text of your message. Terminate each line with <RETURN>. When you have finished the text of the message, type an end-of-file character (usually ^D), or a full-stop character. You should now return to your normal shell prompt. If the message is dispatched successfully, you will hear no more about it. The following is example of the mail command in action:

% mail  lnp6ttld
Subject: UNIX course
I don't think I'll ever be able to get the students
in the UNIX course to understand how to use e-mail.

Entering the text of the message by this method is a rather crude process. Errors on the line being typed can be erased with your delete key, but once you have pressed <RETURN>, a line cannot be edited. A message may be aborted by pressing ^C twice.


Send yourself a message. (You will find out where it has gone in the next section.)

Subcommands while entering mail

There are several commands you can type while entering mail:

<CTRL/Z> will cancel the message, and leave the text in a file named dead.letter.

^e invoke a text editor to edit your message.

~v invoke a screen editor to edit your message.

~f reads the contents of the message you have just read, into your message text.

~r file reads contents of file into your message text.

While this method is quick and easy to use, and quite adequate for short and simple messages, many users prefer to first create a file containing the text of the message, and then mail this file to the intended recipient. This enables you to use any system editor and formatter to create the message, and you do not need to send it immediately.

The following sequence shows how to send a file note containing the text of a message to another user.

%  mail  lnp6ttld  <  note

To understand fully how this works see the section on 'Re-direction of standard output' in Chapter 8 below.

In this example the message will not contain a subject heading, unless one has already been included as the first line of the file note. There is a -s option with the mail command, that can be used to include a subject header, as follows:

%  mail  -s  UNIX  lnp6ttld  <  note

The string following the -s is the subject; in this case, the subject is "UNIX".

Receiving mail

If new mail is waiting for you when you login, you will see the message:

You have new mail

To start the mail program type the command:

%  mail

Each message is summarised on a numbered list. The current message is marked with a ">" character. The mail prompt character is "&". Type the number of the message you want to read, or just press <RETURN> to read through the list. The list of mail headers will look something like this:

%  mail
Mail version SMI 4.0 Thu Oct 11 12:59:09 PDT 1990  Type ? for help.
"/usr/spool/mail/lnp5jb": 2 messages 2 new
>N  1 lnp5mw      Thu Jan  9 15:10   11/262   hello
 N  2 lnp5js      Thu Jan  9 15:11   10/287   party

This tells Jenny Brown that she has two messages, one from user lnp5mw, and one from lnp5js. The date and time at which the messages were received is also listed, and so is the subject header (the last item on each line - here 'hello' and 'party'). The following commands can be entered to the mail prompt:

d Mark the current message for deletion

d n Mark message number n for deletion

u n undelete message number n.

w file save the current message in file with the mail header and mark for deletion

s file Save the current message in file without the mail header and mark for deletion

r Reply to the current message

q Quit mail, removing deleted messages from your system mailbox. Undeleted messages that have been read are normally stored in your personal mailbox (see below)

x Exit mail, leaving your mailbox untouched, i.e. messages deleted in this session are restored

h Show list of message headers

? List the useful mail commands

! command Execute specified shell command

- Re-read previous message.

m recipient Send mail to named recipient

Files used by mail

~/mbox Your personal mailbox, located in your home directory. This is where messages that you have saved are stored, unless you specified another location when you saved them. You can access this file by issuing the command:

%  mail  -f  mbox

~/.mailrc A file that can hold commands for mail to obey when it starts up.


See if you have received any mail. If you have, save a message to your mailbox file. Send yourself another message, and this time discard it. Send a message to another user.

Sending mail to remote users

The following also applies to the elm mail program.

Sending mail to users on other computer systems is simple using mail. Simply type the full address of the remote user where the system username is used above. For example:

%  mail  lnp5mw@uk.ac.leeds.gps
or% mail -s Hello ecl6rsh@uk.ac.leeds.cms1 < note

These two examples show two ways of sending mail shown above.

It is also possible to use mail to look at folders of mail that you have already received. To do this type:

%  mail  -f  folder_name

and it will treat the messages in the folder as incoming mail.

Sending on-line messages

As you have seen, messages sent using mail are received in a special buffer, and it is up to the recipient when to look at them and what to do with them. It is also possible to send a message that will simply appear on the screen of the recipient, if they are logged on. This is less useful than mail for the following reasons:

mail can be used irrespective of whether the recipient is logged on or not.

mail messages can be stored by the recipient. This means that files can be transferred by mail, and a record of transactions can be kept.

On-line messages can be confused with whatever the recipient has on screen and can easily disrupt what the are doing. They can be very annoying!

On the other hand, on-line messages do have the advantage of obtaining the immediate attention of another user, and it is possible to have an interactive conversation. Bearing these facts in mind, use the following command with caution!


The write command is used to send on-line messages to another user on the same machine.

The format of the write command is as follows:

% write username
text of message

After typing the command, you enter your message, starting on the next line, terminating with the end-of-file character. The recipient will then hear a bleep, then receive your message on screen, with a short header attached. The following is a typical exchange. User lnp5jb types:

% write lnp8zz
Hi there - want to go to lunch?

User lnp8zz will hear a beep and the following will appear on his/her screen:

Message from lnp5jb on sun050 at 12:42
Hi there - want to go to lunch?

If lnp8zz wasn't logged on, the sender would see the following:

% write lnp8zz
lnp8zz not logged in.

SunOS has the talk command. This has several advantages over write. Firstly, talk can call other machines on a network. Secondly, talk provides a clearer interface for the exchange of messages, dividing the screen into two windows for the interlocutors. Type

talk  username@machine

to start a conversation.


Try to have an extended on-line conversation with another user.

You can stop messages being flashed up on your screen if you wish. To turn off direct communications type:

%  mesg  n

It will remain off for the remainder of your session, unless you type:

% mesg y

to turn the facility back on. Typing just mesg lets you know whether it is on or off.

Remote logins

It is possible to log on to another machine on a Unix network, provided that you have permission to do so. To do this use the rlogin command. Type:

rlogin machine

and you will be asked for your password. It may be necessary for you to do this to make on-line communications with another user easier.


1. Send a message to another user on your Unix system, and get them to reply.

2. Create a small text file and send it to another user.

3. When you receive a message, save it to a file other than your mailbox. (Remember you can always send yourself a message if you don't have one.)

4. Send a message to a user on a different computer system.

5. Send a note to your course tutor telling him that you can use mail now.

[Next Chapter] [Top page] [Previous Chapter]