As others have mentioned the hash is a associative array (key --> value) that Bash maintains so that when a command is executed, Bash searches this hash first to see if the command's location on disk has already been found via $PATH
, and stored there for quicker searching.
You can preload the hash by giving a list of commands that you want Bash to find when it's invoked. This variable is called BASH_CMDS
.
excerpt from man page
BASH_CMDS
An associative array variable whose members correspond to the
internal hash table of commands as maintained by the hash builtin.
Elements added to this array appear in the hash table; unsetting
array elements cause commands to be removed from the hash table.
Additionally if you look at the Bash man page there is a section titled, COMMAND EXECUTION which details the state machine that Bash uses when a command is typed at the prompt.
excerpt
If the name is neither a shell function nor a builtin, and contains no
slashes, bash searches each element of the PATH for a directory con‐
taining an executable file by that name. Bash uses a hash table to
remember the full pathnames of executable files (see hash under SHELL
BUILTIN COMMANDS below). A full search of the directories in PATH is
performed only if the command is not found in the hash table. If the
search is unsuccessful, the shell searches for a defined shell function
named command_not_found_handle. If that function exists, it is
invoked with the original command and the original command's arguments
as its arguments, and the function's exit status becomes the exit
status of the shell. If that function is not defined, the shell prints
an error message and returns an exit status of 127.
You can find out what's currently in your hash using the -l
switch.
Example
$ hash -l
builtin hash -p /usr/bin/rm rm
builtin hash -p /usr/bin/sudo sudo
builtin hash -p /usr/bin/man man
builtin hash -p /usr/bin/ls ls