I found a nice little script which iterates through a couple of manuals for a specific command. As a last resort, it will Google it and open a browser window. Source here: http://www.commandlinefu.com/commands/view/7581/rtfm-function

rtfm() { help $@ || $@ -h || $@ --help || man $@ || xdg-open "http://www.google.com/search?q=$@"; }

The problem is, if I search for a manual which matches a builtin command partly, this will be found and displayed, not the actual command. Example:

rtfm tr

will find and display the help for the trap command which is clearly not what I am looking for.

Thus, how to prevent the help command from finding commands which don't exactly match the command I'm looking for? Is it even possible to do this?


Since help is the only one of the commands that works with partial matches, you can just invert the order of the commands:

rtfm() { "$@" -h || "$@" --help || man "$@" ||
         help "$@ "|| xdg-open "http://www.google.com/search?q=$@"; }
  • But that will show the man page instead of the help for the builtin if a builtin has the same name as an external utility. – Gilles Apr 30 '15 at 20:49
  • @gilles yes, but the OP's version would show the help and not the man page. Granted, a more sophisticated approach like the one in your answer is better, but this will serve for most cases since the vast majority of commands are not builtins. – terdon May 1 '15 at 10:47
  • @terdon What proportion of commands are builtins is irrelevant, since help wouldn't work on the ones that aren't anyway. It's only for names that both have a man page and are a builtin that the order matters, and then help is preferable. – Gilles May 1 '15 at 15:21

Test what kind of command the argument is. This has the added benefit of correctly detecting aliases and functions that might shadow external commands.

rtfm () {
  declare x
  for x; do
    case $(type -t "$x") in
      alias) alias "$x";;
      keyword) LESS="$LESS+/^SHELL GRAMMAR" man bash;;
      function) type "$x";;
      builtin) help "$x";;
        man -S 1,8 "$x" ||
        "$x" --help ||
        xdg-open "http://www.google.com/search?q=$x";;
      '') # no such command
        man "$x" ||
        xdg-open "http://www.google.com/search?q=$x";;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.