Bash doesn’t search
PATH every single time you invoke a command. It maintains an hashmap instead, with all of the commands launched in the current session, and a count of how many times each command has been run. There is a Bash builtin meant to be used to interact with that hashmap: hash .
$ hash hits command 1 /sbin/dmesg 1 /usr/bin/host 1 /bin/ls 1 /usr/bin/top 1 /usr/local/bin/docker
If you want to reset a single voice in the table you can run
$ hash dmesg #resets counter for dmesg
Use case 1: Reset messed up ENV
$ hash -r #clears the hashmap
When you use sandboxed environments like python’s VirtualEnv, you may end up in situations where you are running executables outside of the sandbox because PATH have been updated but the command is still in the hasmap, you can solve by clearing the hashmap
Use case 2: Executing command outside of PATH
$ hash -p filename name
You can even use hash to run executables not in the currant PATH without the need to put in in the path (useful if you don’t want to put the whole folder containing a lot of other executables).