Where to start First steps track · 15 stages
Linux Labs

hash — Bash command path cache

4 min Ubuntu 24.04; Bash 5.2 for shell builtins

A Bash builtin for inspecting and clearing remembered paths to external commands.

Cheat sheet

5 commands Printable version
  • type hash check the command kind
  • help hash builtin help
  • hash display remembered paths
  • hash -t ls show cached ls path, if present
  • hash -r clear this shell's table

Overview

hash manages Bash's table of remembered external command paths. When finding a program, the shell does not always need to scan every PATH directory again. This is local shell state, not a list of installed packages.

The name does not refer to calculating file checksums. The command does not clear DNS, browser caches, or terminal history either. In a lesson about PATH it helps distinguish a saved path from the current search.

Options

5

Without arguments, hash displays the table. -t prints the path for a remembered name, -d removes one entry, and -r clears the table. Querying an entry that has not been created can fail.

Manually binding a name to a path with -p changes command resolution and requires separate understanding. Inspection and clearing are enough for ordinary diagnostics. Use help hash for the current shell's documentation.

Option What it does Example
-r Reset (clear) the whole table of remembered paths. hash -r
-d name Remove a single name from the table. hash -d ls
-t name Print the remembered path for a name. hash -t ls
-l Print the table in a form that can be reused as input. hash -l
-p pathname name Explicitly bind a name to a path, skipping the PATH search. dangerous hash -p /usr/bin/ls ls

Nothing found. Try a shorter query.

Recipes

3

Investigate stale command resolution

bash
type -a ls
hash -t ls
hash -r
type -a ls

Compare the results. Clearing the cache does not install a program or add a PATH directory; if the problem remains, continue checking command search and execution permissions.

Remove just one entry

bash
hash ls
hash -t ls
hash -d ls

The first call remembers the discovered path, the second displays it, and the third removes the entry. The program file remains in place.

Fix the correct shell context

Clear the cache in the shell where the problem occurs. Running bash -c 'hash -r' separately clears a child shell's table and does not alter its parent.

Troubleshooting

3

An unexpected command still runs after clearing

The name may identify a function or alias. Check type -a ls: an external path table does not describe every form of Bash name resolution.

The table is empty

This is valid, for example after clearing it. Inspect type ls and shell settings. An empty cache alone does not mean external programs are missing.

Another terminal did not change

Another shell has its own context. Repeat the observation where the failure occurs instead of editing system files at random.

Danger zone

hash -r deletes neither programs nor user data. It is also not a universal fix for command not found: an incorrect PATH, a typo, and a missing file require their own fixes.

Do not manually bind a name to an unverified executable. Changing how a familiar name resolves can cause different code to run with your privileges.

See also

  • which — finding out what the shell will execute.
  • env-vars — PATH and process environment.
  • bash — builtins and child shells.
Loading…