Can I get individual man pages for the bash builtin commands?

16,725

Solution 1

Try this:

bashman () { man bash | less -p "^       $1 "; }

You may have to hit n a couple of times to get to the actual command instead of a paragraph that happens to have the command name as the first word.

Explanation: this pipes the entire output of man bash, i.e. bash's entire man page (which is a huge document, and has subsections explaining each bash builtin command) to the reading program less. less' -p flag stands for "pattern"; what it does is automatically scroll to the first point in the input text that matches the pattern. The pattern here is a regex which matches "The start of a line (^), followed by a specific number of spaces, followed by ..." – and here, bash inserts the first argument provided to the bashman function, because bash sees the special $1 token (which means "the first argument") in a string delimited with double-quotes (single quotes would tell bash that you literally mean the characters $1). So, if you run bashman cd, you will effectively be searching for any line in bash's man page with starts with a bunch of spaces, then the string "cd". Because there might be other points in bash's entire man page that also match this pattern besides the actual heading of the section that explains, eg., "cd", this function may not actually take you to the correct part of the bash man page.

Solution 2

help read
help read | less

In zsh:

run-help read

or type read something and press M-h (i.e. Alt+h or ESC h).

If you want to have a single man command so as not to need to know whether the command is a built-in, define this function in your ~/.bashrc:

man () {
  case "$(type -t "$1"):$1" in
    builtin:*) help "$1" | "${PAGER:-less}";;     # built-in
    *[[?*]*) help "$1" | "${PAGER:-less}";;       # pattern
    *) command -p man "$@";;  # something else, presumed to be an external command
                              # or options for the man command or a section number
  esac
}
Share:
16,725
Tyilo
Author by

Tyilo

Lol

Updated on September 18, 2022

Comments

  • Tyilo
    Tyilo over 1 year

    Is there anywhere you can download a manpage for every builtin commands?

    I know you can just use help or man bash and search to find info about it, but I want them separated, so I can just do man read and get the read manpage.

    • mattdm
      mattdm almost 13 years
      It's not quite what you want, but on my Fedora 15 system, these are separated into separate man pages which reference a builtins (1) man page. This is still a big aggregate document, but at least it's just the builtins and not everything to do with bash.
    • Tyilo
      Tyilo almost 13 years
      Doesn't work in Mac OS X
    • suspectus
      suspectus over 10 years
      Nor does man builtins work on linux mint.
    • Loves Probability
      Loves Probability over 7 years
      If all that you need is to know about a built in, Just use help <BuiltinName> -- Hope it helps those people like me annoyed on the failure of man and info with famous builtins. E.g. help command to know about the great yet less used command command. Finally as the question also hints, the help alone simply lists all possible builtins. (Verified on Ubuntu 16.04).
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 13 years
    Good idea. Not what I think Tyilo wants, but I'm not convinced I got that right.
  • Tyilo
    Tyilo almost 13 years
    Works perfect! Adding a space after $1 makes it better
  • Chris Perkins
    Chris Perkins over 12 years
    type -t gives and empty string for a pattern. How does this work? *[[?*]* ?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    @balki type looks up an exact name. I don't think there's a way to look up a pattern, short of having a hard-coded list of built-ins and doing some complicated parsing of the output of alias, typeset -f and $PATH lookups.
  • thomanil
    thomanil almost 11 years
    You can also use LESS=-p"^ $1 " man bash. That way, you're not stripping any escape sequences.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 7 years
    @spex No, I meant “i.e.”. It's followed by a complete list, not by some examples.
  • spex
    spex almost 7 years
    @Gilles your list is not exhaustive and not universal, they are examples, thus e.g.
  • Admin
    Admin over 6 years
    run-help does display help for built-in, if it's not built-in it will open the man-page for that tool. Good tip.
  • Luke Davis
    Luke Davis about 6 years
    You can also create a function that wraps around the original man function: function man() { local binman=/usr/bin/man; if ! $binman $1 &>/dev/null; then echo "No man entry for \"$1\"."; elif $binman bind | grep "BSD General Commands Manual" &>/dev/null; then LESS=-p"^ $1 " $binman bash; else $binman $1; fi; }.
  • rugk
    rugk over 5 years
    Actually, help set and run-help set, all present exactly the same as man here! So this is no solution. Note I am using zsh.
  • rugk
    rugk over 5 years
    As I've explained here too this often does not work. As you stated, you often get to "a paragraph that happens to have the command name as the first word". As such, this question is not completely answered…
  • Jonathan Komar
    Jonathan Komar about 4 years
    Ironically, man help fails, haha! It requires a recursive-looking call help help.
  • ijoseph
    ijoseph about 2 years
    Similar for zsh: zshman () { man zshbuiltins | less -p "^ $1 "; }
  • Admin
    Admin almost 2 years
    instead, I do this: $man bash builtins - opens a dedicated man page. Then search e.g. for shopt [. Advantage of this approach is that is a method so it's immediately transferable. Disadvantage is that you have to remember it;)