Bash hash builtin command

Updated: 05/04/2019 by Computer Hope
hash command

On Unix-like operating systems, hash is a built-in command of the bash shell that views, resets, or manually changes the bash path hash. The path hash is a hash table, maintained by bash, containing the disk locations where the shell should look for executable programs when a command is run.

Description

When you run a command, bash looks up its location on disk in the hash table. This table of locations was previously determined when the shell was launched, according to the PATH environment variable in the shell's init script, e.g., .bashrc or .bash_profile.

The hash table is maintained automatically, but you can force it to re-calculate its program locations with the hash command.

Syntax

hash [-l] [-r] [-p pathname] [-d] [-t] [commandname ... ]

Determine and remember the full pathname of each commandname. If no arguments are given, display information about remembered command locations.

Options

-d Forget the remembered location of each commandname.
-l Display information in a format that can be used as input for another program.
-p pathname Use pathname as the full path of commandname.
-r Forget all remembered locations.
-t Print the remembered location of each commandname. If multiple commandnames are given, precede each location with corresponding commandname.
commandname Each commandname specified is searched for in the PATH environment variable, and if found, is added to the list of remembered commands.

Exit status

The hash command returns 0 for success. Any other value indicates commandname not found, or invalid option given.

Examples

hash

Display information about the hash table. If there is no pertinent information, this command displays nothing.

hash -r

Forget all remembered locations, and determine them right now. If you run a command and bash can't find it (because you made a change to your system, for example), run hash -r and try the command again.

hash -d which

Forget the remembered location of the which executable (typically /usr/bin/which) and determine it right now.

hash -t which

Print the remembered location of the which command.

/usr/bin/which

alias — Define aliases (alternative names) for commands.
which — Determine the location of the program associated with a given command.