stealthyoda
stealthyoda

Reputation: 61

What does hashed mean

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.

Upvotes: 3

Views: 813

Answers (3)

Kharthigeyan
Kharthigeyan

Reputation: 690

You can manipulate the hash table using the hash built-in command in bash. This is useful if you have two versions of the same program installed, you can hash one binary file to be used with the bash session.

# List
hash -l 

# Delete a path in the hash table
hash -d python3

# Add a new path, which may be another version of python3
hash -p /usr/bin/python3 python3

Upvotes: 0

Esmatullah Hanif
Esmatullah Hanif

Reputation: 63

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.

Upvotes: 2

chepner
chepner

Reputation: 532093

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.

Upvotes: 2

Related Questions