Finding an Executable through the Search Path

A shell can search an ordered list of directories to resolve an executable name. A different environment can select a different program with the same name.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace two installations
  2. Record the exact tool
  3. Trace which executable is selected
  4. Distinguish aliases and built-ins from executable files

Trace two installations

If a project tool appears in two directories, the search order can pick a version available first. A process environment can describe the path passed by the launcher.

Record the exact tool

For a rebuild or investigation, record the selected executable and its version. A run from automation may inherit a different environment. See toolchain settings.[1]

Trace which executable is selected

Suppose two tools named format exist in separate directories. Under an ordinary PATH search, the order of those directories determines which executable is found, subject to the shell's lookup rules. Inspect the resolved path and version before comparing two runs. See selecting a script interpreter.

PATH searches ToolsA before ToolsB
Both contain format
A bare command can select ToolsA's binary
An explicit path can select the intended binary

Distinguish aliases and built-ins from executable files

A shell's command resolution may consider functions or aliases before PATH lookup. Use the shell's supported inspection mechanism. Verify the version in the actual environment, including a noninteractive runner.

References

  1. The Open Group, POSIX Shell Command Language ↩