What is the value, proper use, and behavior of a command's "absolute path"?

We have many questions that address an absolute path, sometimes for files, sometimes for commands. I want a central knowledge base question about what exactly an absolute path does as it relates to commands (not only files).

What does an absolute path for commands achieve and how does it affect work in Unix & Linux?

  • How does an absolute path behave differently from a relative path?
  • Does the absolute path override the environment $PATH setting?
  • Does anything additional happen in command substitution: $(/bin/echo Hello world)
  • Is an absolute path better in scripting?
  • Why are there sometimes many absolute paths for a command?
    • /usr/bin/echo
    • /bin/echo
    • Both work on the same system
    • Is one more consistent across distros or preferred by POSIX?
  • Why do some commands, like cd not have any absolute path?
  • Why would or would not or should we use an absolute path in scripting?
  • Do shell interpreters care about absolute paths, such as bash vs zsh?

Disambiguation

  • This asks about any given command’s:
    • Absolute path (/usr/bin/echo)
    • In contrast to its relative path (echo)
  • I’m not asking about canonical paths as they relate to symlinks.
  • I am not asking about absolute vs relative vs canonical paths or their basic definition (ie: POSIX filename with / slash indicating directory tree, etc)
Asked By: Jesse

||
  • How does an absolute path behave differently from a relative path?
  • Does the absolute path override the environment $PATH setting?
  • Does anything additional happen in command substitution: $(/bin/echo Hello world)

Both absolute and relative paths override $PATH. What matters is whether a / is present. If it is /some/command or ./some/command or some/command, the command at that location, whether absolute or relative, will be executed, instead of being looked up in $PATH. If it is some-command without a slash, it will be looked up in $PATH. Command substitution doesn’t affect this.

  • Why are there sometimes many absolute paths for a command?

This depends on the system. On Linux systems with UsrMerge (even though the page mentions Fedora, this inlcudes recent Ubuntu, Debian, Arch Linux, etc.), /bin is a symlink to /usr/bin, so /bin/echo and /usr/bin/echo are the same. On Solaris, more standards-compliant utilities are provided in the XPG sections: When to use XPG* version of a command? Other systems might have other reasons.

  • Why do some commands, like cd not have any absolute path?

Sometimes they do: What is the point of the `cd` external command?, sometimes they don’t: Why is cd not a program?


As for all the "should"s and "shouldn’t"s, it depends on what you want to do. Sometimes I have seen people use absolute paths because they think it will keep them safe from any alternate version of tools that a user might have installed. Sometimes I have seen this cause trouble to people trying to port software to another system where the tools at those absolute paths aren’t the expected ones, but the alternate versions installed by the user would have worked.

Answered By: muru
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.