hash(1) hash(1)
NAME
hash - process shell hash table
SYNOPSIS
hash [name ...] Format 1
hash -r Format 2
DESCRIPTION
The Bourne 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 com-
mands that are invoked under their basic file names (basenames). When-
ever you invoke a command under its basename, the shell first searches
the hash table for your command. This accelerates the search process
[see sh(1)]. 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.
In the Korn shell ksh(1), hash is an exported alias for alias -t -;
alias(1) is a Korn shell built-in command.
Format 1: Display or extend hash table
hash [name ...]
name Basename of a command, an executable shell script, or an execut-
able program. The shell searches for this file by examining the
contents of the PATH variable. When the file is found, the fol-
lowing information is passed to hash:
- the appropriate relative path name in the form ./name if the
current directory is assigned to the PATH variable and con-
tains name, or
- the absolute path name.
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:
- shell scripts for which you have no execute permission.
Page 1 Reliant UNIX 5.44 Printed 11/98
hash(1) hash(1)
- commands with a name that includes a slash. This means that
you cannot enter absolute or relative path names.
- shell built-ins, since they are subroutines of sh or ksh and
thus do not have names that represent executable files.
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:
The hash command writes the contents of the hash table on the
standard output. The output is structured as follows:
hits cost command
1 1 /bin/mail
1 2 /usr/bin/tar
1 1 /bin/ls
1 1 /bin/cat
1 1 /bin/chmod
The meanings of the entries in the above columns are given
below:
hits The number of times the shell has executed this command
since it was entered in the hash table. The digit 0
identifies commands that have so far only been entered,
but not yet executed.
An asterisk immediately after the number identifies
executable files that are not located in the default
directories for PATH, i.e. are not contained in /bin or
/usr/bin.
cost A measure of how many path names in the PATH variable
were searched by the shell in order to find the speci-
fied command.
command Absolute path name of the executable file, or a path
name in the form ./name for a file in the current
directory.
Format 2: Delete hash table
hash -r
-r 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.
Page 2 Reliant UNIX 5.44 Printed 11/98
hash(1) hash(1)
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 com-
mand, 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 MESSAGES
name: not found
This error message may be produced for any of the following reasons:
- name is not contained in any of the directories whose respective
paths have been entered in the PATH variable.
- name is actually contained in one of these directories, but is not
executable.
- name is a shell built-in or a shell function.
ENVIRONMENT VARIABLES
PATH Search path of the shell
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
Page 3 Reliant UNIX 5.44 Printed 11/98
hash(1) hash(1)
EXAMPLES
Example 1
Display the contents of the hash table:
$ echo PATH
/bin:/usr/bin:.
$ hash
hits cost command
1 1 /bin/mail
1 2 /usr/bin/tar
2 1 /bin/ls
3 1 /bin/cat
1* 3 ./stich
1 1 /bin/uname
Example 2
Add a command to the hash table:
$ hash lpr
$ hash
hits cost command
.
.
0 1 /bin/lpr
.
.
This means that the /bin/lpr command has just been entered in the hash
table and has not yet been executed. It was found by the shell in the
directory whose path name was specified first in the PATH variable.
Example 3
The following excerpt from a session demonstrates when the hash table
is cleared:
$ hash
hits cost command
1 1 /bin/mail
1 2 /usr/bin/tar
2 1 /bin/ls
3 1 /bin/cat
0 1 /bin/lpr
1* 3 ./stich
1 1 /bin/uname
Page 4 Reliant UNIX 5.44 Printed 11/98
hash(1) hash(1)
$ sh
$ hash
hits cost command
$ who
...
$ ls
...
$ hash
hits cost command
1 1 /bin/who
1 1 /bin/ls
$ CTRL-D
$ hash
hits cost command
1 1 /bin/mail
1 2 /usr/bin/tar
2 1 /bin/ls
3 1 /bin/cat
0 1 /bin/lpr
1* 3 ./stich
1 1 /bin/uname
1 1 /bin/sh
The hash table in the new subshell is empty. When the subshell is ter-
minated, the hash table of the parent shell becomes valid again, while
that of the subshell is deleted.
NOTES
hash exists both as an external command (/usr/bin/hash) and as a
built-in shell command in the Bourne shell sh(1). The shell generates
a new process to execute /usr/bin/hash.
Some differences in behavior may occur when using hash, depending on
which command is being used. The possible differences are not
described specifically.
SEE ALSO
alias(1), sh(1).
Page 5 Reliant UNIX 5.44 Printed 11/98