Should PATH contain directories or full paths to binaries?

PATH works only with directories, not with single files

From the POSIX standard (emphasis mine)

PATH
This variable shall represent the sequence of path prefixes that certain functions and utilities apply in searching for an executable file known only by a filename. The prefixes shall be separated by a colon ( ‘:’ ). […] The list shall be searched from beginning to end, applying the filename to each prefix, until an executable file with the specified name and appropriate execution permissions is found.

When you type in ls into your shell and your PATH is set to /usr/local/bin/ls:/usr/bin/ls then your shell will …

  1. … look for an executable with the path /usr/local/bin/ls/ls (note the double ls at the end).

  2. As that path does not exist on your system your shell will proceed to look for an executable with the path /usr/bin/ls/ls (double ls again). That path also doesn’t exist.

  3. The shell couldn’t find an executable using all paths in PATH so your shell will print something like bash: ls: command not found.

So, what did we learn? Paths listed by PATH have to be directories. You cannot list single files. Therefore the correct answer in your case is PATH=/usr/local/bin:/usr/bin.


Where things get interesting

Imagine the following situation. You have two versions of program c1 and two versions of program c2. The different versions are stored in the directories /a/ and /b/.

/a/c1
/a/c2
/b/c1
/b/c2

How can we set PATH to prefer /a/c1 over /b/c1/ but at the same time /b/c2 over /a/c2?

Sadly, there is no way to achieve this directly as we can only specify directories in PATH. We have to move/rename some files or create symlinks and use the symlinks inside the paths. One possible solution:

mkdir /c
ln -s /a/c1 /c/c1
ln -s /b/c2 /c/c2
export PATH=/c:/a:/b

The trailing :/a:/b is not really necessary here. I included them under the assumption that /a and /b contain more executables than just c1 and c2.

Leave a Comment