Linux – Does bash have a cache to remember where it last found a binary?

Does bash have a cache to remember where it last found a binary?… here is a solution to the problem.

Does bash have a cache to remember where it last found a binary?

Ok, so I’m playing with my system. I copied the GNU word count program :

cp /usr/bin/wc /usr/bin/wcx

Then I run wc on a text file on my desktop and get the following result, as expected:

53 79 907 /home/me/Desktop/mytextfile.txt

There are currently no anomalies. Then I delete (remove) wc

rm /usr/bin/wc

The command returns with no terminal output. So it has been removed.

Now I should notice that the PATH in this terminal is:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/bin:/sbin:/bin:/usr/games: [ETC]

Now when I run the same command :

wc /home/me/Desktop/mytextfile.txt

I’m back:

bash: /usr/bin/wc: No such file or directory

Now, apparently it shouldn’t be able to find the wc binaries I deleted. This is to be expected. But oddly enough, bash seems to have “remembered” where it last found the program in PATH, because it indicates that it can’t find it in /usr/bin

Is this what bash actually does? Is there a cache where, once it searches for a binary once in the PATH, it knows to return that place for future requests for that binary?

Thanks for helping with this!

Solution

Yes, bash has an internal hash table that remembers the full path of previously used commands. For more details, see man bash and search for hash

Related Problems and Solutions