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
$PATHsetting? - 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
cdnot have any absolute path? - Why would or would not or should we use an absolute path in scripting?
- Absolute paths are relevant in scripting
- Do shell interpreters care about absolute paths, such as
bashvszsh?
Disambiguation
- This asks about any given command's:
- Absolute path (
/usr/bin/echo) - In contrast to its relative path (
echo)
- Absolute path (
- 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)
echois executed by its relative path is not correct. You'd have to use something like../echofor this to apply.echois also a bad example since it is also a shell built-in. Using/usr/bin/bashuses the absolute path, usingbashsearches the $PATH variable, which may or may not include relative paths.catcan be executed as/usr/bin/cat,$HOME/bin/cat, or as./catdepending on how$PATHis 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 atechowhich 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.