What does hashed mean when applied to a path in linux or Mac bash?
When I use the command in bash:
type python3 I get: python3 is hashed (/usr/local/bin/python3)
What does hashed mean. Sometimes I get hashed and sometimes just the path line.
CodePudding user response:
Theoretically, every time you type a command name like foo
that doesn't include a /
, the shell looks at each directory in your PATH
variable to find a command named foo
to execute.
This is somewhat time-consuming and redundant (your commands don't really move much), so the shell only performs the full PATH
lookup once, and caches the result. Typically, it uses a hash table so that command names can be looked up quickly, so "python3 is hashed (/usr/local/bin/python3)" is short for
python3 was found in the hash table and mapped to the path /usr/local/bin/python3
The difference between foo is bar
and foo is hashed (bar)
is that in the former, foo
hasn't been executed yet; type
itself does not store the result in the hash table on a successful lookup.
CodePudding user response:
It's a performance thing; instead of searching the whole path for the binary every time it is called, it's put into a hash table for quicker lookup. So any binary that's already in this hash table, is hashed. If you move binaries around when they're already hashed, it will still try to call them in their old location.
See also help hash, or man bash and search for hash under builtin commands there.