Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm going through http://mywiki.wooledge.org/BashGuide/CommandsAndArguments and came across this:

$ type rm
rm is hashed (/bin/rm)
$ type cd
cd is a shell builtin

Just a little earlier, the guide listed the various types of commands understood by Bash: aliases, functions, builtins, keywords and executables. But there wasn't mention of "hashed". So, in this context, what does "hashed" mean?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

It's a performance thing; instead of searching the whole path for the binary every time it is called, it's put into a hash table for quicker lookup. So any binary that's already in this hash table, is hashed. If you move binaries around when they're already hashed, it will still try to call them in their old location.

See also man hash, or man bash and search for hash under builtin commands there.

share|improve this answer
hash [-lr] [-p filename] [-dt] [name]

For each name, the full file name of the command is determined by searching the directories in $PATH and remembered. If the -p option is supplied, no path search is performed, and filename is used as the full file name of the command. The -r option causes the shell to forget all remembered locations. The -d option causes the shell to forget the remembered location of each name. If the -t option is supplied, the full pathname to which each name corresponds is printed. If multiple name arguments are supplied with -t, the name is printed before the hashed full pathname. The -l option causes output to be displayed in a for- mat that may be reused as input. If no arguments are given, or if only -l is supplied, information about remembered commands is printed. The return status is true unless a name is not found or an invalid option is supplied.

Straight from the horse mouth.. Attribution man hash

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.