hash
Remember and locate executable program locations
TLDR
View cached command locations for the current shell
Clear the hash table
Delete a specific command from the hash table
Print the full path of command
Display help
SYNOPSIS
hash [-lrtd] [-p filename] [-v command ...]
PARAMETERS
-d
Forget the remembered location of each command
. If command
is not specified, forget all remembered locations.
-l
Display the remembered locations in a format that can be reused as input. Typically used for recreating the hash table.
-p filename
Associate command
with filename
. Useful when you have a command with the same name in different locations and want to explicitly use a certain one.
-r
Forget all remembered locations. The shell will search $PATH
for each command again upon next execution.
-t
Write the remembered location of each name
to standard output. If a name
is not found, nothing is displayed.
Useful for script development.
-v
Print the location of each command
that would be executed.
command ...
Commands to hash. If no arguments are supplied, the command displays the contents of the hash table.
DESCRIPTION
The hash
command in Linux is a built-in shell command used to remember the full pathnames of executable files. When you enter a command, the shell has to search your $PATH
environment variable to find the executable. This search process can take time, especially if the executable is located in a directory that is low in the $PATH
list. The hash
command stores the full path of frequently used commands in an internal hash table. Subsequent calls to these commands will bypass the path search, leading to faster execution.
You can use hash
to view the contents of the hash table, add new commands to it manually, or clear the table. It's a useful tool for optimizing shell performance, especially in scripts or interactive sessions where certain commands are executed repeatedly.
CAVEATS
The hash
command only affects the current shell environment. When a new shell is started, the hash table is empty. The -p
option may not work as intended if the specified filename is not an absolute path.
RETURN STATUS
Returns 0 unless a command
is not found or an invalid option is supplied.
EXAMPLES
hash ls
: Add the ls
command to the hash table.hash -r
: Clear the hash table.hash
: Display the contents of the hash table.
HISTORY
The hash
command has been a standard part of Unix-like operating systems for many years. Its purpose has always been to improve command lookup performance within the shell. Initially, its behavior and available options varied slightly between different shell implementations (e.g., Bourne shell, C shell, Korn shell). Over time, POSIX standards helped to define a more consistent interface, though subtle differences may still exist in specific shell implementations.