Linux Command Reference ยท Shell Builtins
Linux hash command
Reference for the Bash hash builtin: how Bash caches program locations, how to list, add, forget and clear entries, and why a moved program is not found.
Searching every directory in PATH each time you run a program would be slow, so Bash remembers where it found each external command. This memory is the hash table, and the hash builtin lets you inspect and change it. The first time you run ls, Bash searches PATH, finds /bin/ls, and stores the result; later runs go straight to that path.
You rarely need hash, until something moves. If you install a new version of a tool in an earlier PATH directory, or delete the one Bash remembered, the shell may keep using the stale path. hash -r forgets everything and fixes it.
hash # list remembered programs hash -r # forget all
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
hash [-lr] [-p PATHNAME] [-dt] [NAME ...]
| Name | Description |
|---|---|
| (no arguments) | List remembered commands with hit counts. |
NAME ... |
Search PATH for each name and remember the result. |
-r |
Forget all remembered locations. |
-d NAME |
Forget the entry for NAME. |
-p PATHNAME NAME |
Remember PATHNAME as the location of NAME, without searching. |
-t NAME |
Print the remembered path of NAME. |
-l |
List in a format that can be reused as input. |
Returns: 0, or non-zero if a NAME is not found in PATH or is not in the table (for -t and -d).
Edge cases: hash with an empty table prints hash: hash table empty and still returns 0. hash -t on a builtin reports not found.
Usage notes
- Stale command symptoms. "No such file or directory" for a program you know was moved, or the old version running after an upgrade, usually means a stale entry. Run
hash -r(orhash -d name). - Assigning
PATHclears the cache in Bash, so changingPATHin the same shell is safe. Stale entries come from files that moved or from programs installed while the shell was running; other shells may not clear the table, so runhash -rwhen in doubt. - Only external programs. Builtins, functions, aliases and keywords bypass the table; see
typeandcommand. - Hit counts show how often each cached path was used by this shell session. They are a curiosity, not a monitoring tool.
- Check before running.
hash toolreturns non-zero when the tool is not installed, butcommand -vis the more standard way to ask. - Disable caching.
set +h(thehashalloption) turns off remembering; scripts that must always searchPATHcan use it. - Per shell. Every shell and subshell has its own table; a new login starts empty. Scripts run in a child process never see your interactive cache.
- zsh and other shells use
rehashinstead;hash -ris the POSIX spelling.
Related
type,which,command,env, Common environment variables- Lessons: Environment, Getting help, Shell basics