Under what circumstances is the PATH
rehashed in bash? I can't figure out when it's necessary to call hash -r
and when it happens automatically. The best I can figure out is that the PATH is rehashed whenever a command is typed that isn't found.
1 Answer
bash does not hash all contents of PATH
. It only has a hashtable of commands that were used during this session (aka in this shell process). For example:
$ hash hits command 1 /home/grawity/code/bin/todo $ ls boot/ dev/ etc/ home/ usr/ ... $ hash hits command 1 /usr/bin/ls 1 /home/grawity/code/bin/todo
So hash -r
is only necessary if you used a program at least once in the current shell, and later moved it elsewhere.
-
OK, that clarifies things a bit, though I still don't get when rehashing happens automatically.– asmeurerMar 11, 2014 at 0:51
-
2@asmeurer: as far as I know, bash never automatically rehashes unless shell option
checkhash
is set and the hashed filename no longer exists. (And then it only rehashes the one name, not the whole hash.)– riciMar 11, 2014 at 1:29 -
1