Here's a hypothetical situation....There are two binaries with the same name. Lets call them charles. One charles is in the /usr/bin/ directory and the other charles is in ~/bin/. Assuming that ~/bin/ is included in the execution path, which charles will be executed when the charles command is called?
-
In addition to the answers, don't forget that you can always prefix the command with the full path in order to select the binary you want to run. Doing so will override the order given in $PATH. (Yes, I know this is more than a comment but was deleted as an answer. I hope it helps.)– LatkarFeb 19, 2022 at 7:36
2 Answers
If issued just the command it will be the one that is found first in your $PATH
. If you do
echo $PATH
you get the path that is used to find an executable...
/usr/local/sbin:/usr/local/bin:/usr/sbin:
/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
If ~/bin/
- is NOT in $PATH it wont be found.
- is before
/usr/bin/
the one in~/bin/
is used; - is behind
/usr/bin/
the one in/usr/bin/
is used;
Extra exception:
If you are in the ~/bin/
directory and do ./charles
the one in ~/bin/
will be executed.
This really depends on where /usr/bin and /bin and ~/bin are in the exported PATH variable.
EX:
PATH=/usr/bin:/bin:~/bin
-----> charles will be found in /usr/bin and used from there
PATH=~/bin:/usr/bin:/bin
-----> in this case charles will be found first in the users' bin
and used from there
-
-
There are several places. Have a look at /etc/login.defs and search for ENV_PATH. You'll find the minimal value for every user logged into the system. In addition you can check ~/.profile, /etc/profile and some of the flles under /etc/profile.d/. You'll find too that there's a value for PATH (and maybe others environment variables) hardcoded inside the bash ELF itself.– LatkarFeb 23, 2022 at 18:20