It was almost three years ago when I wrote about manual pages. Moving forward, I want to share a simple and useful shell script to search through manual pages.

Shell script

Store shell script.

# search through man pages
# Source:
# Usage:
# manual_name search_pattern
# Examples:
# ^tar absolute
# fopen fopen\( 

# use colors: always, never

# display surrounding lines

if [ "$#" -eq 2 ]; then

  manuals=$(man -k $1) # Important:
                       # Use "-k" parameter or "-f" if you want to be strict

  echo "$manuals" | while read manual_line; do
    manual_page=$(echo $manual_line | sed 's/\(.*\) (.*) [ ]*- .*/\1/')
    manual_section=$(echo $manual_line | sed 's/.* (\(.*\)) [ ]*- .*/\1/')

    matches=$(man $manual_section $manual_page 2>/dev/null | grep --color=$grep_colors -C $grep_lines -e "$pattern")

    if [ -n "$matches" ]; then
      echo "$matches" | while read match; do
        echo "$manual_page ($manual_section): $match"

Use grep_colors variable to use colors and grep_lines to define the number of surrounding lines.


$ sh ^tar absolute
tar (1):
tar (1): -P, --absolute-names
tar (1): don't strip leading '/'s from file names

$ sh ^test$ newer
test (1): FILE1 -nt FILE2
test (1): FILE1 is newer (modification date) than FILE2

$ sh fopen fopen\(
fopen (3):
fopen (3): FILE *fopen(const char *path, const char *mode);
fopen (3):
fopen (3): --
fopen (3): DESCRIPTION
fopen (3): The  fopen() function opens the file whose name is the string pointed to by path and associates a stream with
fopen (3): it.
fopen (3): --
fopen (3): pointed  to by stream with it.  The original stream (if it exists) is closed.  The mode argument is used just
fopen (3): as in the fopen() function.  The primary use of the freopen() function is to change the file associated  with
fopen (3): a standard text stream (stderr, stdin, or stdout).
fopen (3): --
fopen (3): RETURN VALUE
fopen (3): Upon  successful  completion  fopen(),  fdopen()  and  freopen()  return  a FILE pointer.  Otherwise, NULL is
fopen (3): returned and errno is set to indicate the error.
fopen (3): --
fopen (3): ERRORS
fopen (3): EINVAL The mode provided to fopen(), fdopen(), or freopen() was invalid.
fopen (3):
fopen (3): The fopen(), fdopen() and freopen() functions may also fail and set errno for any of the errors specified for
fopen (3): the routine malloc(3).
fopen (3):
fopen (3): The fopen() function may also fail and set errno for any of the errors specified for the routine open(2).
fopen (3):
fopen (3): --
fopen (3): CONFORMING TO
fopen (3): The fopen() and freopen() functions conform to C89.  The fdopen() function conforms to POSIX.1-1990.
fopen (3):
fopen (3): --
fopen (3):
fopen (3): x      Open  the  file  exclusively  (like  the O_EXCL flag of open(2)).  If the file already exists, fopen()
fopen (3): fails, and sets errno to EEXIST.  This flag is ignored for fdopen().
fopen (3):
fopen (3): In addition to the above characters, fopen() and freopen() support the following syntax in mode:
fopen (3):
fopen (3): --
fopen (3): When parsing for individual flag characters in mode (i.e., the characters preceding the "ccs" specification),
fopen (3): the  glibc implementation of fopen() and freopen() limits the number of characters examined in mode to 7 (or,
fopen (3): in glibc versions before 2.14, to 6, which  was  not  enough  to  include  possible  specifications  such  as
fopencookie (3):
fopencookie (3): The fopencookie() function serves a purpose similar to fopen(3): it opens a new stream and returns a  pointer
fopencookie (3): to a FILE object that is used to operate on that stream.
fopencookie (3): --
fopencookie (3):
fopencookie (3): The  mode  argument serves the same purpose as for fopen(3).  The following modes are supported: r, w, a, r+,
fopencookie (3): w+, and a+.  See fopen(3) for details.
fopencookie (3):
fopencookie (3): --
fopencookie (3): SEE ALSO
fopencookie (3): fclose(3), fmemopen(3), fopen(3), fseek(3)


How to open manual page at the specific page section blog post