HowTo: Linux / UNIX Create a Manpage

by on May 6, 2010 · 4 comments· LAST UPDATED May 13, 2010

in

How do I create a man page for my shell or python script under Linux / UNIX operating systems?

Almost all UNIX like oses comes preinstalled with man pages.

Troff and Groff Macro

troff is a document processing system developed by AT&T for the Unix operating system. The troff typesetting system includes sets of commands called macros that are run before starting to process the document. These macros include setting up page headers and footers, defining new commands, and generally influencing how the output will be formatted. Under Linux all new manual pages should be marked up using the groff an.tmac package. The groff (GNU troff) software is a typesetting package which reads plain text mixed with formatting commands and produces formatted output.

A Quick Note About Man Page Layout

All man pages follow a common layout and it is recommend that you use the same for your man pages too:

NAME
    The name of the command or function, followed by a one-line description of what it does.
SYNOPSIS
    In the case of a command, you get a formal description of how to run it and what command line options it takes.
DESCRIPTION
    A textual description of the functioning of the command or function.
EXAMPLES
    Some examples of common usage.
SEE ALSO
    A list of related commands or functions.
BUGS
    List known bugs.
AUTHOR
   Specify your contact information.
COPYRIGHT
    Specify your copyright information.

You can add a few more other sections such as EXIT STATUS, ENVIRONMENT, FILES, and HISTORY etc. The table below shows the section numbers of the manual followed by the types of pages they contain.

Man Page Sections

The manual is generally split into eight numbered sections, organized as follows under Linux or UNIX like oses:

SectionDescription
1Executable shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]

To see options and section information you can use with command man, enter the following command:

 
man man
 

Man Page Location

The system stores its man pages at /usr/share/man/ directory as described in about section. For example, the directory /usr/share/man/man1 stores man pages for user shell commands. You can view it by typing the following command:

cd  /usr/share/man/man1
ls -l
zcat ls.1.gz

Custom Man Page Location

It is recommended that you store your own man pages in /usr/local/man directory. You can set man search path in /etc/man.config file:

MANPATH /usr/man
MANPATH /usr/share/man
MANPATH /usr/local/man
MANPATH /usr/local/share/man
MANPATH /usr/X11R6/man

See manpath man page for more details about how to determine search path for manual pages:
man manpath

How Do I Create My Own Man Page?

The groff (GNU Troff) software is a typesetting package which reads plain text mixed with formatting commands and produces formatted output such as man page. It comes with various macro packages such as man and mandoc to create man pages. Create a file as follows
$ vi nuseradd

.\" Manpage for nuseradd.
.\" Contact vivek@nixcraft.net.in to correct errors or typos.
.TH man 8 "06 May 2010" "1.0" "nuseradd man page"
.SH NAME
nuseradd \- create a new LDAP user
.SH SYNOPSIS
nuseradd [USERNAME]
.SH DESCRIPTION
nuseradd is high level shell program for adding users to LDAP server.  On Debian, administrators should usually use nuseradd.debian(8) instead.
.SH OPTIONS
The nuseradd does not take any options. However, you can supply username.
.SH SEE ALSO
useradd(8), passwd(5), nuseradd.debian(8)
.SH BUGS
No known bugs.
.SH AUTHOR
Vivek Gite (vivek@nixcraft.net.in)

Save and close the file. To view your man page, enter:

man ./nuseradd

Sample outputs:

Creating a Linux Manual (man) Page

Fig.01 Man page in action


The .TH macro introduces a manual page (e.g., set man page title and section). Section headers, paragraph breaks, lists and displays etc displayed using .SH macro. See the following man page which describes all macros:
man 7 mdoc

How Do I Install My Man Page?

Simply type the following command:

cp nuseradd /usr/local/man/man8/nuseradd.1
gzip /usr/local/man/man8/nuseradd.1
man nuseradd

You can also use install command as follows (recommend for shell scripts):

install -g 0 -o 0 -m 0644 nuseradd.1 /usr/local/man/man8/
gzip /usr/local/man/man8/nuseradd.1

References:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 VIKAS July 18, 2012 at 11:45 am

Hi Vivek,

Excellent article. I just learnt to write man pages for my shell scripts.

I just found one mistake, I believe below lines

cp nuseradd /usr/local/man/man8/nuseradd.1
gzip /usr/local/man/man8/nuseradd.1

should be

cp nuseradd /usr/local/man/man8/nuseradd.8
gzip /usr/local/man/man8/nuseradd.8

Regards,
Vikas

Reply

2 randy March 1, 2013 at 12:03 am

Very nice. I am just getting feet wet with my own scripts. I had started to put man style text right in script files and cat them out on demand for –help. This a so much better and fun!
Hint: peek into the files at /usr/share/man to get to know more about write the markup the man pages use.

Reply

3 Greg February 21, 2014 at 3:40 pm

Thank you for writing this tutorial about creating man pages. I encountered an issue when deploying the man page which I created. When I ran man my_script it reported “No manual page for my_script.” I found the step missing in your otherwise excellent introductory-level tutorial; the ‘mandb’ command must be executed before man my_script displays as anticipated. Hopefully, you read these comments and update your tutorial.

Reply

4 David Gamba April 7, 2014 at 2:00 pm

Thanks for the article, was looking for the right place to put the man file.
I Usually write my man pages in Asciidoc format (asciidoctor.org) and then convert them to different outputs depending on need, in general html and troff. Another cool thing about it (besides the fact that you write once in an easy to write/read format and publish in many) is that github understands Asciidoc documents (.adoc or .asciidoc) so you can use that same write up for your README and if not, then you can read the documentation for your script without having to even generate the html output.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: