Replace “man” by “?”
Domanda
I am using R a lot. In R, you can call a man page for a function by writing
?print
I got used to that so much that I intermittently type that in a bash shell window as well. Now, I have set up an alias
?='man'
that almost cuts it: I can do, for example, ? ls
. However, I would prefer it to work with ?ls
. Unfortunately, this one does not work. Is there any way to make bash call the man page when I type the question mark without the space after?
Soluzione
bash
provides a hook for handling undefined commands. Since you are unlikely to ever have any commands whose names start with ?
, you can use this hook to process any attempt to run such a command:
command_not_found_handle () {
if [[ $1 =~ ^\? ]]; then
cmd=${1#\?}
man $cmd
else
echo "$1: command not found" >&2
return 127
fi
}
This function would go into your .bashrc
file, so that is it available in any shell. When you try to execute
$ ?ls
the command is not found, the hook intercepts the failed attempt to find the command, determines that the command name starts with ?
, then strips the ?
and passes the result as an argument to man
. Other undefined commands merely produce an error message similar to the default bash
error for unfound commands and exits with status 127 (the same status bash
usually uses to denote command not found).