Part II · Your system
23.3 PATH and command resolution
alsopath$pathwhichcommand -vtypehash -rcommand resolution.local/bin
TL;DR
- PATH is a list of folders separated by
:. The shell looks for a command in each, left to right, and runs the first match. type NAMEsays what a name is: alias, function, builtin or file.type -ashows every match.- Your own scripts usually go in
~/.local/bin. Many distros add it to PATH when it exists; otherwise add it in a startup file (27.2).
echo "$PATH" | tr : '\n'
type -a python3
export PATH="$HOME/.local/bin:$PATH"
On your machine
$ atfq 23.3 PATH /home/alice/.local/bin /usr/local/bin (doesn't exist) /usr/bin . ⚠ the current folder, wherever you are Repeated /usr/bin (listed more than once: harmless, the first one wins) Libraries LD_LIBRARY_PATH=/opt/oracle/lib Runs programs built for x86-64, glibc
An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.
Pitfalls
- bash remembers where it found each command: after installing or moving
one,
hash -rmakes it search again. - An empty entry or
.in PATH means "the current folder": a file namedlsplanted there would run instead of the real one. whichis a separate program that doesn't know your aliases and functions;typetells what your shell will actually run.sudouses its own PATH (secure_pathin sudoers): a command your shell finds can be "not found" under sudo.
Deep dive 2 questions
In what order does the shell look?
For a name without a /: aliases first, then functions, then builtins
(cd, echo), then the PATH folders. A name with a / in it, like
./build.sh or /usr/bin/python3, is a path: no search at all.
Why /usr/local/bin before /usr/bin?
So that a program you install by hand wins over the packaged program of the
same name, without touching the files the package manager owns. The same
goes for ~/.local/bin placed first: your version wins, for you only.