Writing and reading man pages

The Man to Know

Article from Issue 185/2016
Author(s):

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 /dev, configuration files, and drivers

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.

Figure 1: If you consult man pages regularly, installing whichman can help you find relevant pages.

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.
Figure 2: The SYNOPSIS or basic command structure for the cp command, showing several variations.
  • 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).
Figure 3: The SEE ALSO section lists additional sources of information.

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.

Figure 4: A selection of options for the cp command.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Command Line

    Do have difficulty remembering

    command syntax? It often pays to

    check the manpage. In this issue of

    Command Line,we look at how to

    create your own manpages,and

    how to convert manpages to other

    formats.

  • Admin Workshop: Documentation

    System admins routinely refer to documentation for help with obscure command line options. In this month’s Admin Workshop, we examine the programs and formats behind the command line documentation you read everyday.

  • Introducing Bash

    Beyond all the splash screens, screen savers, and vivid rock-star wallpaper is the simple yet powerful Bash shell.

  • Command Line: Editing xorg.conf

    Understanding xorg.conf makes it easy to tweak your graphical display setup.

  • Moodle CMS

    The Moodle course management system is useful for e-learning and for practical work in the computer room.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News