LinuxCommandLibrary

hash

Remember and locate executable program locations

TLDR

View cached command locations for the current shell

$ hash
copy

Clear the hash table
$ hash -r
copy

Delete a specific command from the hash table
$ hash -d [command]
copy

Print the full path of command
$ hash -t [command]
copy

Display help
$ hash --help
copy

SYNOPSIS

hash [-r] [-d name] [-t name ...] [-l] [name ...]

PARAMETERS

(no options)
    Displays the contents of the shell's hash table, showing hit counts and full paths of remembered commands.

name
    Locates the command name in the PATH and adds its full path to the hash table. If name is already hashed, its entry is updated.

-r
    remove: Clears all entries from the shell's hash table, forcing the shell to re-search the PATH for subsequent command invocations.

-d name
    delete: Removes the entry for name from the hash table. If name is not found in the table, no action is taken.

-t name [name ...]
    tell: Displays the full path to name (or multiple names). If a command is not found in the hash table or the PATH, an error message is printed and the command exits with a non-zero status.

-l
    list: Displays the contents of the hash table in a format that can be re-input as commands (e.g., hash -t /bin/ls). This option is often combined with other options or used for scripting.

DESCRIPTION

hash is a fundamental shell built-in command designed to optimize the execution speed of frequently used commands. When you type a command, the shell typically searches through directories listed in your PATH environment variable to locate the executable. This search can introduce overhead, especially with a long PATH.

hash mitigates this by maintaining an internal hash table. Once a command's location is found during the initial execution, its full path is stored in this table. Subsequent calls to the same command can then retrieve its path directly from the hash table, bypassing the potentially time-consuming PATH search. This significantly speeds up command invocation.

Beyond caching, hash also allows users to inspect the current hash table, add new entries, or remove specific commands or the entire cache, providing control over this performance optimization mechanism.

CAVEATS

hash is a shell built-in command, meaning it is part of the shell itself (e.g., Bash, Zsh) and not an external executable file. Its behavior and available options can vary slightly between different shells. The hash table only stores paths for commands found within the user's PATH environment variable.

If an executable's location changes after it has been hashed, the cached path will become stale, leading to errors until the entry is removed (using hash -d or hash -r) or the shell is restarted.

EXIT STATUS

The hash command returns an exit status of 0 (success) unless an invalid option is provided, or if the -t option is used and the specified command name is not found either in the hash table or in the PATH.

PERFORMANCE IMPACT

While hash significantly speeds up subsequent executions of commands, the initial lookup still involves a PATH search. For commands executed only once or very rarely, the performance gain is negligible. Its primary benefit is for frequently used commands in busy shell sessions.

HISTORY

The concept of command hashing for performance optimization has been a fundamental feature of Unix-like shells for decades. It's not a standalone utility but rather an integral part of shells like Bash, Zsh, and others, designed to improve the interactive user experience by reducing command lookup times. Its core functionality has remained consistent over time.

SEE ALSO

type(1), which(1), whereis(1), command(1), PATH(7), bash(1), zsh(1)

Copied to clipboard