-1

I notice that when I compile a custom build of a packaged program stored in /usr/bin eg /usr/bin/emacs, which emacs shows the already existing executable at /usr/bin/emacs instead /usr/local/bin/emacs, although /usr/local/bin occurs earlier in my PATH.

I need to log in and log out, or open a new screen frame before which emacs shows the new binary.

Is that because of some caching, or is there some safety feature that causes the existing path to be referenced until logged in again?

EDIT:

For those who consider this question to be a duplicate or answered by other questions eg - How do I clear Bash's cache of paths to executables?, What is the purpose of the hash command?, this question asks specifically about a shell, not about bash, or the hash commandbuilt into bash (which I didn't know about) , inspite of it being the default shell on most systems.

There are other shells in existence eg, zsh the one I use, fish, ksh, eshell, ksh and host of other new shells, eg oilshell coming on to the market, so to speak.

So I think this question should stand on its own, being about shells in general, not one particular shell or the other.

If there is a rationale for this, experience has taught that it is the sensible option, or it has to do with economy or performance than other answers may elaborate on it.

I would have preferred this edit to be a comment, but they are limited in length.

vfclists
  • 7,531
  • 14
  • 53
  • 79
  • 1
  • "this question ask specifically about the 'shell'" -- except that as you say, there's more than one shell! How they implement command name resolution is up to them, so a question like this necessarily has to be shell-specific – ilkkachu Nov 07 '22 at 13:21
  • @ilkkachu Actually I noticed many tutorials asking the user to log out and log in again to enable some commands in my early days of Linux, but I didn't know what was going on, just following the instructions and it was bash back then.

    But of late I'm doing a lot of compilation into /usr/local which require me to start a new shell to use the newly installed versions, and I am also using zsh which is why I see it as a shell thing, not a bash or zsh thing.

    – vfclists Nov 08 '22 at 10:57
  • 1
    The problem is that this answer will be a little different for each shell you might ask about. Different shells can handle caching differently and some shells don't even implement it. There isn't a general answer for all shells. – penguin359 Nov 11 '22 at 09:04

1 Answers1

1

Yes, bash and other shells maintain a "hash" table of commands that it has looked up by trying everything in your PATH variable, so it doesn't have to check every time. People making changes, as you are, to the environment will find the command hash -d NAME useful, or the hash -r. These are bash commands, other shells might do things somewhat differently.

So,

hash -d emacs

will ask bash to forget the emacs path, and

hash -r

will ask it to forget all paths. I tend to use the latter.

Hack Saw
  • 1,024