I was watching a Primeagen video on YouTube, and he used the command hash -r
while coding. I think he probably installed bun
and then ran hash -r
to ensure bun
was accessible in the terminal. This caught my attention because I usually just run source ~/.bashrc
to ensure the binary is accessible in my terminal.
Hash Command
Typically, when you run a command, Bash searches the directories in your PATH
environment variable to find an executable file that matches the command. It then stores this location in a hash table to speed up future executions of the same command by not having to search through the PATH
again.
In Bash, the hash
command is used to manage the hash table of remembered locations of commands. Essentially, it keeps track of the paths to executable files that have been looked up, which speeds up the lookup of these files during subsequent command executions.
hash -r
So using hash -r
clears all entries from this hash table, making Bash recheck the PATH
the next time any command is executed.
How is it different from source command
The source
command (or its shorthand, .
, as in . script.sh
) is used to execute commands from a specified file in the current shell session. This means that any variables, functions, and changes to the environment made by the script will affect the current shell.
When you source
a file, Bash reads and executes all commands in that file as if they were typed into the terminal. This is particularly useful for initializing environment variables, functions, and other settings that need to be applied to the current session.