-3

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)
Jesse
  • 351
  • 1
    40 years ago, I had been instructed to always refer to commands by their absolute path when logged as root. I have kept this habit. I remember having commented that way some QA here… which triggered some… amusement from other knowledgeable contributors. So… apart from some obvious-to-me security reasons, particularily when sysadmin of OS-devs dedicated systems is shared… let's acknowledge that using absolute path for commands is a matter of… personal idiom ;-) – MC68020 Mar 13 '24 at 21:44
  • 2
    Hi, I believe that the answer you link to about the difference between relative and absolute paths actually fully defines what an absolute path is! Could you elaborate on what questions specifically remain open? – Marcus Müller Mar 13 '24 at 21:50
  • @MarcusMüller I think my question is more about its use and behavior, not so much the definition of its static existence. I'll make an edit to that effect. Thanks! – Jesse Mar 13 '24 at 22:18
  • @MC68020 With examples, that could be an actual answer. I remember four years ago when I went back through all my shell code and changed it to relative paths, even inside command substitution, for that security reason specifically. – Jesse Mar 13 '24 at 22:19
  • 1
    Your statement that echo is executed by its relative path is not correct. You'd have to use something like ../echo for this to apply. echo is also a bad example since it is also a shell built-in. Using /usr/bin/bash uses the absolute path, using bash searches the $PATH variable, which may or may not include relative paths. – doneal24 Mar 15 '24 at 17:25
  • @doneal24 I want to clean up this question, but what is the correct term, then? If /usr/bin/cat is absolute path, what is just cat? I originally categorized it as a kind of relative because it is relative to $PATH. So, I'm wrong, but what is it then? Normal? Command? – Jesse Mar 16 '24 at 01:05
  • I'm beginning to think that I have about five questions here that need to be asked as five questions rather than four in the post plus one in the comments. @doneal24 plz help. – Jesse Mar 16 '24 at 01:05
  • 1
    @Jesse I don't think that an unqualified command actually falls into this discussion. The command cat can be executed as /usr/bin/cat, $HOME/bin/cat, or as ./cat depending on how $PATH is defined. The distinction may fall into how a shell interprets a command, possibly using $PATH in its interpretation. It is even more murky when you look at echo which is usually a shell built-in but can also be called as /usr/bin/echo. Even worse is the semantics of the built-in and the shell are different. – doneal24 Mar 16 '24 at 19:00
  • @doneal24 not trying to troll, but I'm working with an education curriculum and I need to be able to talk about this with students. So, it would be okay to say, "The absolute path to the command /usr/bin/cat should run the plain command cat." I think I could say plain command in a similar situation without causing a problem, from what you say. Thank you, while it seems unimportant to most. – Jesse Mar 16 '24 at 19:21
  • [mods: please don't delete these comments, I need to come back and clean this up when I get time and want the comments for reference] – Jesse Mar 16 '24 at 19:23

1 Answers1

4
  • 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.

Jesse
  • 351
muru
  • 72,889