Previous Table of Contents Next


You are placed in command mode when you run mailx without specifying recipients on the command line. After the messages are displayed mailx prompts you for a command by displaying a ?. At the prompt you can type any of the following commands. The general format of these commands are:

     [ command ] [ message_list ] [ argument(s) ]

If you do not specify a command, the print command is assumed. For example, to display the current message you just press Return. To display message number 2 you type 2 and press Return.

If you do not provide a message, the current message is assumed. If you do specify a message or list of messages, the specified command acts upon each message. For example,


deletes the current message. The command


deletes message number 2.

Some commands require arguments. You specify the arguments after the command and the message lists.

Message List

There are several identifiers you can use to specify a message list. Before we look at the commands, let's step through the possible identifiers you can use in a message list. A msg or msglist may consist of one or more of the following message identifiers.

Identifier Description

. The current message. Preceded by a >
^ The first message. Deleted messages are ignored.
$ The last message. Deleted messages are ignored.
* All messages
n Message number n. The number n must be an integer.
n-m Messages n through m. The numbers n and m must be integers.
user All messages sent to you by the specified user.
/str All messages that have the string str in the Subject field. The match is not case sensitive (str matches uppercase or lowercase equivalents).
:d All deleted messages
:n All new messages
:o All old messages
:r All read messages
:u All unread messages


The following table lists all of the commands you can use while in the command mode of mailx. Some commands only accept one message while others accept multiple messages. To distinguish between these commands we use msg to represent a single message and msglist to represent one or more messages. Any commands that use filenames for arguments may have a plus (+) placed before the filename to force the use of the folder variable.

Command Description

!cmd Escapes to the shell and executes the UNIX command cmd. After the command completes, control returns to mailx. For example,

!ls -x

lists the current directory in column format. Refer to the SHELL variable in the ENVIRONMENT VARIABLE section. Also refer to the bang variable.

# comment A comment. You may find this useful in the .mailrc file.
? Displays a summary of internal mail commands.
= Displays the current message number.
alias alias name(s) Creates an alias string alias for a list of user names. When you use alias as a recipient, all of the users listed are substituted in place of the alias. For example,

alias mfg awk jji

substitutes awk and jji in place of mfg when you send mail to mfg. So the mail is actually sent to awk and jji.

alternates [ name(s) ] Establishes a list of alternate names you use to log in to various systems. When responding to messages using the reply command, these alternate user names are removed from the list of users to whom the message is sent. This prevents you from mailing a message to yourself that one of your alternate accounts previously forwarded. If you do not provide a name argument, the current list of alternate user names is displayed.
cd [ dir ]
chdir [ dir ] Changes directory to the specified dir directory. If you do not specify a directory, $HOME is used.
co [ msglist ] [ file ]
copy [ msglist ] [ file ] Copies the messages you specify in msglist to the given file. The messages are not marked as being saved. If you do not specify a msglist, the current message is copied. If you do not specify a file, the messages are copied to mbox unless you have defined the MBOX variable to a different filename.
C [ msglist ]
Copy [ msglist ] Copies the messages to a file whose name is derived from the sender of each message. The messages are not marked as being saved. An example, ? Copy 1 saves message 1 to the file bill since the message was sent to you by the user bill.
d [ msglist ]
delete [ msglist ] Deletes the messages specified in the msglist. The messages are removed from your system mailbox. If you have the autoprint variable set, the message following the last deleted message is displayed.
di [ header_field(s) ]
discard [ header_field(s) ] Suppress the specified fields of each message header. For example,

di status

causes the status field not to show up when you use the h command. Refer to the section of Header Fields for a list of header fields you may specify.

dp [ msglist ]
dt [ msglist ] Deletes the specified messages in msglist and displays the next message that follows the last message in msglist.
ec string
echo string Echo the given string. Functions like the UNIX echo command. Used in the .mailrc file.
e [ msglist ]
edit [ msglist ] Edit the messages you specified in the msglist. Each message is copied to a temporary file and edited. The name of the editor is the value of the EDITOR variable. If EDITOR is not set, the ed editor is used.
exit Exits from mailx. The mailbox file is not changed. No messages are saved to the mbox file.
file [ file ] Switches mailbox files from the current file to the new mailbox file. For example,

file mbox

causes mailx to quit the current file and read in the mbox file. If you do not specify a file, your system default file (/usr/mail/mylogin) is used. The file name may consist of one of the following special characters.

% Your current mailbox
%user The mailbox belonging to user. You must have read access to the specified user's mailbox file.
# The previous file you were viewing messages from.
& Your mbox file.
folder file Same as the file command.
folders Displays the name of each file contained in your mail directory. A folder is synonymous to a file. Each folder contains messages to and from a user. You can specify the name of your mail directory using the folder variable.
followup [ msg ] Sends your follow-up message to the author of the msg. A copy of the message you send is kept in a file named after the author of the original message. Ignores the value of the record variable.
Followup [ msglist ] Sends your follow-up message to the author of each message in the msglist. Your response is to the first message in the message list. A copy of the message you send is stored in a file named after the author of the first message. The value of the record variable is ignored.
from [ msglist ] Displays the header information for the specified messages.
group alias name(s) Same as the alias command.
headers [ msglist ] Displays the header lines for the specified messages. The number of header lines displayed at one time can be controlled by the screen variable or the z command.
help Displays a brief summary of the more popular mailx commands.
hold [ msglist ] Stores (holds) the specified messages in your mailbox (/usr/mail/mylogin).
en See if, else, end that follow.
if mode
else mail_cmds
endif The send or receive if else command. The mode is either s (send) or r (receive). If the mode is set to s, the commands up to the else or endif (if the else is not provided) are executed. The commands after the else and before the endif are executed if mailx is in the receive mode. If the mode is r then the conditions are reversed. For example,

if s
pipe . lp
discard cc bcc

might be used in the .mailrc file to cause all messages you send to users to be piped to the lp command. If the messages are being read, then the cc and bcc fields are discarded from the header lines.

ignore [ header_field(s) ] Same as the discard command.
list Displays a list of all command mode commands you can use while in mailx. No explanation is given for the commands. Helps jar your memory though.
mail name(s) Allows you to mail a message to the specified user(s). mailx enters the send mode as if you had specified a list of names as arguments on the mailx command line.
mbox [ msglist ] Causes the list of mail messages, msglist, to be copied to the mbox file when you exit mailx using the quit command. You can specify the name of the mbox file by setting the value of the MBOX variable to the desired name.
next [ msg ] Search to the next message matching the specified msg. Refer to the message list section preceding these commands. You use this command to search forward for a message sent by a particular user or a message containing a particular string.
pipe [ msglist ] [ "cmd" ] Pipes the messages you specify in msglist to the standard input of the UNIX cmd command. For example,

pipe . "lp -dlaser"

sends the current message (.) to the UNIX lp command, with a destination printer of laser. If you do not specify a message, the current message is sent. If you do not provide a cmd command, the value of the cmd variable is used. If you have the page variable set, a form feed is inserted between each message. The pipe command flags the message as being read, which affects how other commands perform.

preserve [ msglist ] Same as the hold command.
print [ msglist ] Displays each message to your screen. You can set the crt variable to control how many lines you want displayed per "page." The paging command defined by the PAGER variable is used to page through all of the messages. The default value for PAGER is the pg (more on BSD) command.
Print [ msglist ] Same as the print command except the header fields are displayed even if the discard/ignore command has been used to suppress header fields.
quit Quits mailx. Messages that you have read are saved to the mbox file. Messages that you have not read are returned to your mailbox (/usr/mail/mylogin). Messages marked for deletion are removed from your mailbox and are not saved to any file.
reply [ msg ] Allows you to enter a reply to the author of the specified message. The subject line of the original message is used in your message. Your reply message is also sent to all the other recipients of the original message. If the record variable is set to a filename, then your reply message is saved in the file.
Reply [ msglist ] Allows you to send a reply to the sender of each message specified in msglist. The subject line from the first message is used in your message as the subject line. A copy of your response message is saved in the file specified by the record variable. The Reply command is like the Followup command except Followup saves your response in a file named after the author of the original message, whereas Reply saves your response to a general response file or does not save the file at all.
respond [ msglist ] Same as the reply command.
Respond [ msglist ] Same as the Reply command.
Save [ msglist ] Saves all of the messages in the msglist to a file named after the sender of the first message. Any network addressing is removed from the sender name.
save [ file ]
save [ msglist ] [ file ] Saves the indicated messages to the file you specify. The first form saves the current message to the file. If you do not specify a file, the mbox file is used. Messages you save to a file are deleted from your mailbox unless you have the keepsave variable set.
set Displays all internal variables and their values you have set. You can set these variables in the .mailrc file or in command mode using the set command. See the Environmental Variables section that follows for a full description of each variable.
set name Set the Boolean variable to true. Use the unset command to set the variable to false. For example,

set bang

sets the bang variable to true.

set name=value Set the variable to a specified numeric or string value. You can create variables of your own, then retrieve the value by using the ~i command.
shell Invokes a subshell. By default the UNIX sh shell is invoked. You can specify the shell of your choice in the SHELL variable. To return to mailx after entering a subshell you can type exit and press Return or press Ctrl-D.
size [ msglist ] Displays the size of each message in number of characters.
source file Reads the file and executes each line as a mailx command. After the file has been processed, control returns to your current command mode session. An example would be if you made changes to your .mailrc file while in mailx and you want the changes to take effect without having to exit and re-enter mailx.
top [ msglist ] Displays the first five lines of each message you specify. You can change the number of lines to any number you prefer by setting the toplines variable.
touch [ msglist ] "Touches" each message you specify to be saved to mbox. Makes mailx consider the messages to have been read.
type [ msglist ] Same as the print command.
Type [ msglist ] Same as the Print command.
undelete [ msglist ] Changes the status of deleted messages back to normal. You can only change the status of a message you have deleted in the current session of mailx. Once you exit mailx you cannot undelete a message you deleted in an earlier session. If you have the autoprint variable set, the last message in the msglist is displayed when you perform the undelete.
unset names(s) Unsets or removes the value of the indicated mailx variables.
version Displays the current version of the mailx you are using and the date it was released. Handy for those who just want to know and useful if you need to report problems to your vendor.
visual [ msglist ] Invokes the editor specified by the visual variable. Each file in the msglist is edited. The default editor is vi.
write [ msglist ] file Writes the specified messages in msglist to the file. The header information and trailing blank line are not copied to the file.
xit Same as the exit command.
z[+] Scrolls the list of header lines one screen forward. The screen variable may be set to control the number of lines displayed in a screen.
z- Scrolls the list of header lines one screen backward. The screen variable may be set to control the number of lines displayed in a screen.

BSD (Berkeley)
The BSD mail program does not support the following commands.
followup list
Followup pipe (|)
discard Save
if s | r touch
  mail_cmds version
The BSD mail does, however, support the following commands that are not supported under System V mailx.
retain header_list Opposite of the ignore command. It adds the given header names to the list of headers to retain. When mail displays the header fields only those in the retain list are displayed. With no arguments the list of retained header fields is displayed.
unread [ msglist ] Mark each message in the given list as unread.

Previous Table of Contents Next