hash Command

The hash command manages the shell's internal hash table that stores the locations of recently executed commands. This improves performance by avoiding repeated PATH searches for frequently used commands.

Syntax

hash [OPTIONS] [COMMAND...]

Description

The hash command is a shell builtin that manages a hash table of command locations. When you run a command, the shell searches the PATH to find it, then stores the location in the hash table for faster future access.

Key features:

  • Display current hash table contents
  • Clear the entire hash table
  • Remove specific commands from the table
  • Add commands to the hash table
  • Improve command execution performance

Common Options

Option Description
-r Clear all entries from the hash table
-d Remove specified commands from hash table
-p pathname Use pathname as the full path for command
-t Print the full pathname of each command
-l Display output in a format that can be reused as input

Examples

Display hash table

hash

Shows all commands currently stored in the hash table with hit counts

Clear entire hash table

hash -r

Removes all entries from the command hash table

Remove specific command

hash -d ls

Removes the 'ls' command from the hash table

Remove multiple commands

hash -d ls cat grep

Removes multiple commands from the hash table

Add command with specific path

hash -p /usr/local/bin/python3 python

Associates 'python' with the specific path in the hash table

Show full pathnames

hash -t

Displays the full pathname of each hashed command

Show specific command path

hash -t ls

Shows the full path of the 'ls' command from the hash table

Reusable format output

hash -l

Displays hash table in a format that can be used as input

Force rehashing of command

hash -d python python --version

Removes python from hash table, forcing PATH search on next use

Check if command is hashed

hash -t vim 2>/dev/null && echo "vim is hashed" || echo "vim not hashed"

Checks if vim command is in the hash table

Understanding Hash Table Output

$ hash hits command 3 /usr/bin/ls 1 /usr/bin/cat 5 /usr/bin/grep

The output shows:

  • hits - Number of times the command has been executed
  • command - Full path to the executable

Performance Benefits

Why Hash Tables Matter
  • Speed - Avoids repeated PATH directory searches
  • Efficiency - Reduces system calls for command lookup
  • Caching - Stores frequently used command locations
  • Automatic - Shell manages the table automatically
When to Clear Hash Table
  • New Installation - After installing new versions of commands
  • PATH Changes - When modifying PATH environment variable
  • Troubleshooting - When commands behave unexpectedly
  • Development - When testing different versions of tools

See also