Getting help with man, help and type

Beginner · 9 min

Find out what a command is with type, read builtin docs with help and external docs with man. Includes why man bash can show the wrong Bash version on a Mac.

Before thisAnatomy of a command

Microsoft Developer · 9 min

Getting help with man, help and type

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Microsoft DeveloperReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • type tells you what a name is: a shell builtin, a keyword, an alias, a function, or a file on disk. The answer decides where the documentation lives.
  • help NAME documents Bash builtins like cd and type. It describes the Bash you are running, with no version mismatch.
  • man NAME documents programs on disk such as ls. Its page comes from the first matching manual directory, which follows your PATH on macOS.
  • Many GNU tools accept --help, but BSD tools on macOS usually print a usage line instead. Don't count on it.
  • man -k WORD, also called apropos, searches page descriptions when you know what you want but not what the command is called.

Three questions behind one word

You are about to use a command you half remember. Before you reach for a search engine, ask what the word you typed is. The shell answers that question with a builtin called type, and the answer tells you where the documentation lives.

$ type cd
cd is a shell builtin
$ type ls
ls is /bin/ls
$ type if
if is a shell keyword

There are five kinds of thing a name can be. The shell checks them in this order: an alias, a keyword (like if), a function, a builtin, and finally a program on disk found through the PATH. type -t prints just the kind:

$ type -t cd; type -t ls; type -t if; type -t nosuch
builtin
file
keyword
$ echo $?
1

nosuch printed nothing and made type fail, which is also how a script can test whether a command exists. I defined an alias to see the fourth kind: type ll printed ll is aliased to `ls -l'.

Some names exist twice. type -a lists every match, in the order the shell would pick them:

$ type -a echo printf
echo is a shell builtin
echo is /bin/echo
printf is a shell builtin
printf is /usr/bin/printf

The builtin wins, so typing echo never runs /bin/echo. Builtins have to exist for a reason: cd changes the shell’s own working directory, which a separate program could never do for its parent. This is the first thing to learn about help, because builtins and programs are documented in different places.

help for builtins

help is a builtin that documents the other builtins:

$ help cd | head -4
cd: cd [-L|[-P [-e]]] [-@] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
$ help -d cd
cd - Change the shell working directory.
$ help -s cd
cd: cd [-L|[-P [-e]]] [-@] [dir]

-d gives a one-line description and -s gives just the syntax. help with no arguments lists every builtin. Because help is part of the Bash you’re running, it can’t describe a different version. The cost is that it only covers builtins. Asking about ls fails:

$ help ls
bash: help: no help topics match `ls'.  Try `help help' or `man -k ls' or `info ls'.

man for programs

Programs on disk come with manual pages, read with man. man -w ls prints where the page lives:

$ man -w ls
/usr/share/man/man1/ls.1
$ man ls | col -b | head -6
LS(1)			    General Commands Manual			 LS(1)

NAME
     ls - list directory contents

SYNOPSIS
     ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when]

I piped through col -b only to strip the formatting for this page. At a real terminal man shows a pager: space pages down, / searches, q quits. The number in LS(1) is the section: 1 is commands, 5 is file formats. When a name exists in two sections, say which: man 5 passwd.

The man page can describe a different version

On a Mac this next detail catches people. I have two Bashes: the system one (3.2.57) and Homebrew’s 5.3. Same question, two different PATH settings:

$ man -w bash
/opt/homebrew/share/man/man1/bash.1
$ PATH=/usr/bin:/bin man -w bash
/usr/share/man/man1/bash.1
$ PATH=/usr/bin:/bin man bash | col -b | tail -2
GNU Bash-3.2		       2006 September 28		       BASH(1)

man builds its search path from your PATH. With Homebrew’s directory first, the page is the 5.3 one. Without it, you read the manual for Bash 3.2, which has no EPOCHSECONDS (the 5.3 page mentions it four times, the 3.2 page zero). If a documented feature seems not to exist, check which page you are reading with man -w.

--help and searching

GNU tools usually print a summary with --help. BSD tools on macOS often don’t:

$ ls --help
ls: unrecognized option `--help'
usage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]

The usage line is still useful. When you don’t know the command at all, search the one-line descriptions with man -k (same as apropos):

$ apropos -s 1 copy | head -3
git-checkout-index(1)    - Copy files from the index to the working tree
serialver(1)             - return the serialVersionUID for one or more classes in a form suitable for copying into an evolving class
tiffcp(1)                - copy (and possibly convert) a TIFF file

Results depend on what is installed, so yours will differ. The plain cp(1) page was a couple of lines further down in my run.

About the video

Microsoft Developer’s “How to Get Help in Bash [4 of 20]” is part of their Bash for Beginners series, published 15 February 2023, 9 minutes 12 seconds. The description gives two chapters: help from 0:00 and man from 4:25. It says nothing about type, so that part of the lesson is mine. I couldn’t read a transcript, so I haven’t checked how it handles the version point. The presenter and series are in English by title and description. Nothing in the metadata suggests it assumes Linux, but it’s a general series, so mac-specific points like the BSD --help behaviour are probably missing.

What I did not run

info pages, man -f, and tldr-style cheat sheets were not tested. Output above came from macOS 27.2 in a clean environment (Bash 5.3.20), so installed man pages on your system will differ.

Next

With help and man, you can look things up. Now to get moving: moving around the filesystem.

Check yourself

  1. 01You want the documentation for cd. Does man cd or help cd describe the one you are running?

    help cd. cd is a builtin inside the shell, so its authoritative description is the shell's own help. A man page for cd may be a generic page shared by all shells.

  2. 02What does type -a echo print on a Mac, and what does it tell you?

    It prints a builtin entry and a file entry (echo is a shell builtin, echo is /bin/echo). Two different implementations exist, and the builtin runs first.

  3. 03man bash on your Mac describes GNU Bash 3.2 but bash --version says 5.3. What happened?

    man finds pages along your PATH and the system page for the old Bash was used. With the newer Bash's directory first in PATH you get its 5.3 page.

  4. 04You don't know the name of the command that does a thing. What do you try?

    man -k followed by a word from the task, such as man -k copy, which lists pages whose one-line descriptions match.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark