Writing and reading man pages
The Man to Know
Man pages provide essential information but may seem cryptic if you're not familiar with their structure. We explain how they're organized so you can get the most out of them.
Few features are more closely associated with Unix-like systems than man pages [1]. First used in 1971 [2], man pages quickly became the standard form for online help. Even the GNU Project's preference for help formatted in Texinfo has done little to affect man's popularity, because, more often than not, man pages are available alongside Texinfo files.
Many users have come to take man pages for granted, using only the basic man
command and never considering how they are created or structured. However, even if you never write a man page yourself, knowing how they are organized externally and internally, as well as their conventions, can help you both find and read them more efficiently.
Man Sections
Most man pages are organized in sections or chapters (Table 1). To avoid confusion between files of the same name in different sections, man files take the format NAME.SECTION.bz
(or .gz
). Topics are usually referred to with the file name, followed by the section in parentheses (e.g., mount(8)
).
Table 1
Man Sections
Section Number | Subject |
---|---|
1 |
User commands |
2 |
System calls |
3 |
Library functions |
4 |
Devices |
5 |
Files in |
6 |
Games and screensavers |
7 |
Miscellaneous |
8 |
System administration commands |
9 |
Kernel routines and daemons |
Sometimes, you also see an "l" section for local files generated by the local system administrators. In the past, some topics such as Tcl and the X Window System have also created their own sub-sections with their own naming conventions. However, these are non-standard practices that few modern Linux systems use.
When you enter the man
command, the program displays a page that might be appropriate. Unfortunately, it generally settles on the first one it encounters, a habit that means you might miss useful information unless a page with the same file name is mentioned at the end of a page. You could always add section numbers to the command systematically in the hope of finding more pages, but a better solution is to install whichman
, a command that lists identical file names in different sections (Figure 1). By default, whichman
also lists near equivalents, so that searching for parted
will also returns partx
, paste
, and parec
, but if you run it with the -e
option, it will return only identical names.
Most man pages are stored in /usr/share/man
or /usr/man
. However, some applications may have their own directories for man pages. Assuming you know about these additional directories, you can create a MANPATH
environment variable to include them.
Man Page Section Headings
Over the years, man pages have evolved a standard set of section headings (Table 2). These generally appear on a line by themselves, in uppercase letters.
Table 2
Man Page Structure
NAME |
SYNOPSIS |
CONFIGURATION |
DESCRIPTION |
OPTIONS |
EXIT STATUS |
RETURN VALUE |
ERRORS |
ENVIRONMENT |
FILES |
VERSIONS |
CONFORMING TO |
NOTES |
BUGS |
EXAMPLES |
AUTHORS |
SEE ALSO |
You do not have to include all section headings to compile a man page, and some make sense only for certain subjects, but if you depart from them too far, then any page that you write may not contain the information that readers come to expect. Others are a matter of choice, such as the AUTHORS, which purists consider irrelevant and others a means of getting their rightful credit.
The audience may also help to decide the sections to include. For example, DESCRIPTION, NOTES, and EXAMPLES are more likely to be used if the page is intended primarily for general users. By contrast, sections such as EXIT, STATUS, RETURN VALUE, and ERRORS are more likely to be used for system calls and library functions used most often by developers or system administrators.
However, most man pages include:
- NAME: Includes at least the topic and its section in upper case. The date of the last revision and the function or system call from which a command derives may also be included – if only as Linux or GNU. Typically, tabs distribute the information against the left margin, middle, and right margin of the first line, even if information is repeated.
- SYNOPSIS: The basic structure for the command, with all options displayed in square brackets and in the correct position (Figure 2). Because some options may be incompatible with each other, or some commands may be used for different purposes, the synopsis may include several lines. Variables such as the file name may be underlined.
- DESCRIPTION: A brief explanation of the topic, including the files it interacts with, and its standard input and output. The description also describes typical ways that the topic may be used.
- SEE ALSO (if relevant): Man pages and web pages for related topics, including pages with the same name in a different section, arranged in a comma-separated list (Figure 3).
Additionally, commands (section 1 and 8) should always include a complete set of OPTIONS. The options are generally listed in alphabetical order, ignoring case, so that -l
is followed directly by -L
. Sometimes, for complicated commands with more than one function, the options may be divided into subcategories.
For each option, the Unix-style option of a hyphen and a single letter comes first, followed by an indentation and the GNU-style option of two hyphens followed by one or more words joined by hyphens (Figure 4). On a new indented line, the description of the option is given concisely, but in full detail.
Which other headings you include depends on what is necessary, but remember that man pages aim to be comprehensive, so too much information is preferable to too little.
The Mechanics of Writing man Pages
Man pages are written in a simple markup language using nroff/groff macros (Table 3). Each markup tag begins with a period and is followed by one or two letters. Typically, the markup is placed at the start of a line and continues to be in use until either the next tag or a closing parenthesis – ) – appears. One or two tags comprise pairs of starting and closing tags. Spacing on a line is handled either by tabs or by the markup's macro.
Table 3
Man Markup
Format | Description |
---|---|
.TH |
[name of program] [section number] [center footer] [left footer] [center header] The title/header of the man page, generally written entirely in upper case. Always the first line of a man page. |
.SH |
[text] Section heading. |
.PP |
Creates a line break. Always use to leave a space, rather than leaving a line blank. |
." |
Comment line (for source file). |
.TP |
Indent the text 2 lines below. |
.nf |
Start of pre-formatted text. |
.fi |
End of pre-formatted text. |
.RS |
Start of a relative margin indent. |
.RE |
End of a relative margin indent. |
.B |
Bold weight. |
.I |
Italics. |
/-/ |
A dash. |
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs