It appears that in tcsh
if I have ambiguously named executables in two separate folders in the $PATH
it directs to the one in the most recently created folder. Am I correct? And is this always true?
-
1Can you give an example?– schaibaMay 9, 2013 at 23:01
4 Answers
In general it will be the order it is found in $PATH
since that is the intended usage for $PATH
.
There could be an exception to this rule but that would require that the command is hard coded with a directory to the command in the software. I would consider that bad practice though.
You can't have two files with the same name in the same directory. If there are several versions of prog
, you have one e.g. in /usr/bin
and another one in /usr/local/bin
, and the PATH
environment variable decides which one is picked. This is done by one of the exec(3)
family which looks at the PATH
(most probably execvp(3)
), the shell has no saying here.
-
I meant if both are in the same path... e.g. if you have a folder called NAMD27 and NAMD27b3 in the path and each has an executable
namd2
. How doesexec(3)
pick which one has priority?? (It appears to be the newest one, but the second one comes later in the path, so it could be the order in the path as well...) May 10, 2013 at 1:44
In Solaris, there are typically two directories (actually more than two) that contain different versions of the same name program. An example is ps
which is found in /usr/ucb
and /usr/bin
. If both /usr/ucb
and /usr/bin
appear in your PATH
, the version of ps
that gets executed is whichever one appears first in your PATH
.
All shells traverse the directories in the $PATH
in order, and execute the first command they find. For example, if your path is /usr/local/bin:/usr/bin:/bin
, then the command from /usr/local/bin
is executed if there is one; if there isn't, the command from /usr/bin
is executed, and so on. It doesn't matter in which order the commands were installed.
There's one exception: if you install a program while the shell is already running, the shell may have kept the location of the program in a cache, in which case it will keep executing the old program. For example, if you start tcsh, then you run foo
which is at that point /usr/bin/foo
, then you install another version of the program in /usr/local/bin/foo
and call foo
from that shell again, it will still run /usr/bin/foo
. A newly started shell will pick up /usr/local/bin/foo
. Run the command rehash
to empty tcsh's cache of command locations. In bash and zsh, the equivalent command is hash -r
(zsh also understands rehash
).