The POSIX shell built-in hash has two functions:
it can write the contents of the hash table on standard output or enter the specified command in the hash table (Format 1),
it can delete the contents of the hash table (Format 2).
Each shell maintains its own hash table, in which it enters all commands that are invoked under their basic file names (basenames). Whenever you invoke a command under its basename, the shell first searches the hash table for your command. This accelerates the search process. If the command is not yet in the hash table of the current shell, it is entered there.
When you start a subshell, its hash table is empty. When you terminate the subshell, the parent shell returns with its own hash table, and the hash table of the subshell is deleted.
Syntax
Format 1: |
hash[ name...] |
Format 2: |
hash -r
|
Display or extend hash table
Basename of a command, an executable shell script, or an executable program. The shell searches for this file by examining the contents of the PATH variable. When the file is found, the following information is passed to hash:
The hash command enters this path name into the hash table. If name is not present in any of the directories assigned to the PATH variable, an error message is issued. The following cannot be specified for name:
By the same token, executable shell scripts and commands are not entered in the hash table unless they are invoked under their basenames. name not specified: ls=/usr/bin/ls cat=/usr/bin/cat chmod=/usr/bin/chmod Delete hash table
Deletes the contents of the hash table. If you modify the value of the PATH variable, the contents of the hash table are automatically deleted. When you terminate the current shell, the hash table associated with this shell is also deleted. You should always delete the contents of the hash table in the current shell in the following circumstances: You have created an executable file in a directory dira whose path name is assigned to the PATH variable. Some other directory dirb, also specified in PATH, already contains an identically named command file which already appears in the current hash table. In this case the shell will always execute the older command, even if its directory (dirb) comes after the first one (dira) in the PATH variable. Deleting the hash table with hash -r will cause the shell to execute the first command it finds the next time either of these commands is invoked. In other words, the sequence of the entries in the PATH variable will be the determining factor. The path name of the first command found is now entered into the hash table, i.e. your command will always be executed from now on when you invoke it under its basename. |
Error
name
|
Variable
PATH Search path of the shell |
Locale
The following environment variables affect the execution of hash: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
Display the contents of the hash table:
|
Example 2
Add a command to the hash table:
The command /usr/bin/cat is now a new entry in the hash table. |
Example 3
The following excerpt from a session demonstrates when the hash table is cleared:
The hash table in the new subshell is empty. When the subshell is terminated, the hash table of the parent shell becomes valid again, while that of the subshell is deleted. |