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?
- Absolute paths are relevant in scripting
- Do shell interpreters care about absolute paths, such as
bash
vszsh
?
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)
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, usingbash
searches the $PATH variable, which may or may not include relative paths. – doneal24 Mar 15 '24 at 17:25/usr/bin/cat
is absolute path, what is justcat
? 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:05cat
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 atecho
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/usr/bin/cat
should run the plain commandcat
." 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