How does the PATH environmental variable work?

Hello.

I have a question about how the PATH environment variable works.
I wrote a script in $HOME/bin/gvim.
I want it to be called instead of /usr/bin/gvim,
so I've placed it before in the PATH.

However, it is still the old one that is found.
If I open an other terminal, I have the desired effect: the script in $HOME/bin is found

I don't understand how it's possible to have two terminals where the PATH variable is the same, echo $PATH outputs the same thing, but the two don't find the same script.
Could anyone explain me the subtlety I'm missing here?
Thanks in advance.

after changing the PATH did you re ran the .profile??
if not run it (. .profile) then you will get the desired result..

Thanks for the reply.
Sourcing the .profile did make the difference.

Why do I need to re-run the .profile?

because this will be written inside .profile

export PATH

after changing the PATH just by exporting also you can see the result no need to source the full .profile :slight_smile:

Thanks again for help

Now I know how I came up with two terminals with the PATH, but not the same call
Here is what happened.

I created a script in $HOME/bin/gvim

It was not executable (forgot to chmod +x), but there is another farther in the PATH

I call once gvim. I skips the one not executable, calls the second one.

I changed with 'chmod +x' the first one.

Surprisingly, it still skips the first one, and calls the second one. Except if I open another terminal.

Strange. On a hunch, try hash -l. You set your new path without logging out, but a new shell works like you want. It the old path is hashed, it will take precedence.

A lot of shells cache the results of a PATH search so that they do not continually rescan all of the directories on the PATH looking for stuff. Most of the time this speeds stuff up nicely. But sometimes those caches become a pain. My solution, which I admit is extreme, is to restart the shell. So say I'm running ksh. I do this:
exec ksh
This causes the shell to replace itself with a new copy. ksh calls this particular feature "tracked aliases" and I think that "unalias gvim" would work, but I actually never have tried it. I just restart the whole shell.