POSIX.1-2008 only mandates support for the -k
option of man
, so you can't rely on man
having any -s
option on a POSIX compatible system, and in fact it is absent in both FreeBSD and Dragonfly BSD. That aside, your command should be reasonably portable barring the following issues:
Some older shells do not support $()
for command substitution
Most notably C shell and the original Bourne shell do not support dollar-parentheses expansion. Use ``
instead.
Output of man -k
may differ between operating systems
On OpenBSD at least, there are no spaces separating the command names and the section numbers in parentheses, so it's probably better to delimit by left parenthesis. But beware of command synonyms that look like byacc, yacc (1)
(MINIX), which will produce undesirable results if not further delimited by comma.
man
may only display the first manual page found
Again on OpenBSD at least, the default behavior of man
is to display only the first manual page found. From the man
manual:
-a Display all of the manual pages for a specified section and name combination. Normally, only the first manual page found is displayed.
Note: this is not POSIX compliant behavior.
man -k .
may actually search for the literal .
character
Both man -k .
and man -k ''
should, according to the POSIX specification, match anything in the man
database, but this is not the case in OpenBSD, MINIX and SmartOS.
Piping to tr
is unnecessary
Unless the IFS
variable has been tampered with, the last command of the pipeline is superfluous.
Proposed solutions
Tested OS: Fairly recent versions of CentOS, Kali Linux, Manjaro Linux, openSUSE, Ubuntu, nixOS; OpenBSD, FreeBSD, Dragonfly; SmartOS; MINIX
Tested shells: sh
, csh
, tcsh
, ksh
, bash
, dash
, zsh
There are three ways to search for man pages in a section: man -s
section
, man -S
section
or just man
section
, presented roughly in the order of decreasing support by the OSes. Linux distros seem to prefer the first way, and BSDs the second. The CentOS man
manual also mentions the -s
option is for compatibility with System V, so it is the safest bet.
A general solution that works in all tested OSes except FreeBSD and Dragonfly:
man -a -s 1 `man -s 1 -k '(' | awk -F '[,(]' '{print $1}' | sort`
A targeted solution that works in all tested BSDs:
man -a -S 1 `man -k sec~^1$ | awk -F '[,(]' '{print $1}' | sort`
Simplest solution for a specific OS (OpenBSD):
man -a -k sec~^1$ ; # May contain duplicates
ls ${PATH//:/ }
\$\endgroup\$