2

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 1

6

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.

3
  • OK, that clarifies things a bit, though I still don't get when rehashing happens automatically.
    – asmeurer
    Mar 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.)
    – rici
    Mar 11, 2014 at 1:29
  • 1
    I see now. So hash -r doesn't rehash, it just clears the hash.
    – asmeurer
    Mar 11, 2014 at 19:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .