devutils/ATFQ/book ATFQ git

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 NAME says what a name is: alias, function, builtin or file. type -a shows 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

alice@laptopexample
$ 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

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.

Commands
type which
Paths
/usr/local/bin ~/.local/bin
See also
23.2 ELF and "command not found"
27.2 Shell startup files