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
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
Shows all commands currently stored in the hash table with hit counts
Clear entire hash table
Removes all entries from the command hash table
Remove specific command
Removes the 'ls' command from the hash table
Remove multiple commands
Removes multiple commands from the hash table
Add command with specific path
Associates 'python' with the specific path in the hash table
Show full pathnames
Displays the full pathname of each hashed command
Show specific command path
Shows the full path of the 'ls' command from the hash table
Reusable format output
Displays hash table in a format that can be used as input
Force rehashing of command
Removes python from hash table, forcing PATH search on next use
Check if command is hashed
Checks if vim command is in the hash table
Understanding Hash Table Output
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