60

I use the man command all the time when I want to get information about a specific command. But this doesn't help me too much when that specific command is a shell builtin. For example:

man cd

returns:

No manual entry for cd

My question is: it is possible to make man also work for all shell builtin commands (like cd, alias, history, etc.), and keywords (like if, while, [[, {, etc.)?

4
  • And how come echo is a builtin command but has a man page?
    – Parto
    Mar 26, 2014 at 13:53
  • 5
    @AvatarParto echo is also a system command (executable) located in /bin. You can check this using type -a echo. The same thing happens with time and maybe others. Mar 26, 2014 at 14:31
  • Did you explore info bash yet? Mar 26, 2014 at 19:46
  • You can always write your own man pages. Mar 27, 2014 at 2:58

4 Answers 4

71

The help command when is used with -m option can display information about builtin commands in pseudo-manpage format. For example:

help -m cd | less

will display information about cd command in a format almost exactly like in a manual page.

Starting from this command you can wrap man command in one function in your .bashrc file as follow:

man () {
    case "$(type -t -- "$1")" in
    builtin|keyword)
        help -m "$1" | sensible-pager
        ;;
    *)
        command man "$@"
        ;;
    esac
}

After this man will work also for all shell builtin commands and keywords. For example:

man :

will display:

NAME
    : - Null command.

SYNOPSIS
    :

DESCRIPTION
    Null command.

    No effect; the command does nothing.

    Exit Status:
    Always succeeds.

SEE ALSO
    bash(1)

IMPLEMENTATION
    GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
7
  • Very clever approach! +1
    – phatskat
    Mar 26, 2014 at 15:45
  • 5
    If there are executables masked by builtins, you can specify the section. man time vs. man 1 time
    – OrangeDog
    Mar 27, 2014 at 14:07
  • 1
    ...note although that if you are a zsh user your are not so lucky: bugs.launchpad.net/ubuntu/+source/zsh/+bug/1242108 (no man pages installed)
    – Rmano
    Mar 28, 2014 at 16:55
  • info, help -m, why can't we just have good man pages these days? Anyway, thanks! Apr 27, 2015 at 9:21
  • +1 ... nice ! The behavior of yr function resembles somewhat what occurs through function overloading in C/C++... Only curiously man type does not produce a description of options "-a" or "-t" in Ubuntu 14.04.4.... and yet, they're there !
    – Cbhihe
    Mar 28, 2016 at 18:53
30
man bash-builtins

This contains help snippets for the builtin commands, albeit in a slightly more condensed format than the help equivalent.

2
  • Good to know! It's from section 7 of the manual. I didn't heard about this manual page until now. +1, but I can't accept this answer because it's not exactly what I asked. Mar 26, 2014 at 10:42
  • On macOS High Sierra that command is simply "man builtins" Oct 21, 2018 at 8:22
13

You can install manual pages about using a POSIX system for development as,

sudo apt-get install manpages-posix-dev

It will provide man pages for shell builtins.

$ type cd
cd is a shell builtin

Now try,

$ man cd
CD(P)                      POSIX Programmer's Manual                      CD(P)

NAME
   cd - change the working directory

SYNOPSIS
   cd [-L | -P] [directory]


...
3
  • 3
    I was waiting for someone to came with this answer. But the problem is that this will only work for some builtins - those specified by POSIX. Mar 27, 2014 at 10:42
  • 3
    Bash does not strictly adhere to POSIX unless run in POSIX mode, so these man pages may not be comprehensive, and may be wrong in some circumstances, especially when describing the behaviour of certain features.
    – Chris Down
    Mar 28, 2014 at 3:34
  • @RaduRădeanu yes you are absolutely correct, it will work for some builtins.
    – sourav c.
    Mar 28, 2014 at 4:01
3

This solution works perfectly well but is a bit of a joke as well because the first thing I thought when I read your question was 'Who still literally uses man from the command line? Doesn't everyone just Google the man page they want (so that they get fancy things like unlimited scrolling)?'. Then I realized that the sites I Google usually all have both types of commands so why not just use them to provide a uniform man page interface across all commands. Hence, this fun was born.

This requires an Internet connnection for any entries you haven't already looked up at least once. It also needs these two small apps which are missing in a default installation of Ubuntu:

 sudo apt-get install tidy html2text

These aren't absolutely needed but they do help make it look a little nicer. Tidy will clean the HTML and html2text will format that html as formatted text (which is usually pretty trivial since most of these sites are already text formatted and just wrapped in <pre> tags.

Now all you need to do is add this to the end of ~/.profile:

function iman() {
    if [ ! -d "/usr/share/iman" ]; then
        sudo mkdir -m a=rwx /usr/share/iman
    fi
    if [ ! -f "/usr/share/iman/$1.html" ]; then
        curl "http://unixhelp.ed.ac.uk/CGI/man-cgi?$1"| tidy -n -asxml 2>/dev/null| html2text > "/usr/share/iman/$1.html"; 
    fi
    if [ -f "/usr/share/iman/$1.html" ]; then
        cat "/usr/share/iman/$1.html";
    else
        echo "Entry not found."
    fi
}

After you logout and then back in you should be able to type this:

iman cd

and it'll display the man page for cd.

This uses a data directory (/usr/share/iman) in order to minimize our network requirements (so it'll work for entries you've already found before even without the connection; also to minimize load on this random linux man pages site I found with the system entries we want in it as well). If you don't use this anymore you'll want to remove that to recover disk space.

Hopefully, the rest is pretty straight forward.

6
  • 6
    'Who still literally uses man from the command line?` Me! and everyone else that has to visit a datacenter (no internet ;) )
    – Rinzwind
    Mar 26, 2014 at 14:08
  • Point taken, this won't help you then to be clear.
    – krowe
    Mar 26, 2014 at 14:09
  • 6
    I would think the opposit, I rarely ever use Google to look up bash commands. It's a lot faster to just type "man command" without having to leave the keyboard.
    – laurent
    Mar 26, 2014 at 14:23
  • alt+tab -> alt+d -> command will do the same without mouse and without canceling the current cli command and with scrolling and with a million other nice things a browser gives you. I'm sure you wouldn't know anything about that because your alway use this site and others like it from lynx...ffs
    – krowe
    Mar 26, 2014 at 14:26
  • In case of builtins, your iman function return the same output as man bash-builtins. Mar 26, 2014 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .