简体   繁体   中英

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

Ok, so I am playing around in my system. I copied the GNU word count program:

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

and then I ran wc on a text file on my desktop and got the following, as expected:

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

so far nothing out of the ordinary. I then delete (remove) wc

rm /usr/bin/wc

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

Now at this point I should note 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 get back:

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

Now, obviously it should not be able to find the wc binary as I deleted it. That is as expected. But the curious thing is that bash appears to have 'remembered' where on the PATH it last found the program as it has indicated that it can't find it in /usr/bin

Is this an actual feature of bash? Is there a cache that, once it has searched the PATH for the binary once, it knows to return to that place for future requests of that binary?

Thanks for helping to clear this up!

Yes bash has an internal hash table that remembers the full path to previously used commands. See man bash and search for hash for further details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM