(Published: 2024-10-14)
Hash Fast, Bash Smart
hash is a useful built-in shell command used for caching commands/executables from your $PATH variable. This helps bash/zsh avoid having to rescan your PATH each time you enter a command during the find and execution process.
This command is great for resetting/regenerating the PATH commands cache with the -r flag. For example, python3’s virtualenv uses this to manage your command cache after activating and deactivating your virtual environment.
hash -r 2> /dev/null
This snippet runs hash with -r flag to “reset” the commands cache. Error/logging is discarded to /dev/null for quiet execution.
Note: Running
hashalone just prints out the current command cache list
Use Cases
- (Shown above) Quickly recaching your commands after (de)activating virtualenv
- Moving, renaming, or deleting a command: Reflect its non-existence with
-r - Installing a package/binary: Quickly recache to shell with
hash -r - After adding a new directory into
PATHand wanting to index its commands
To learn a bit more about hash, check out the bash manual entry.