Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

$PATH Command Guide: Where the Shell Looks for Commands

$PATH is the environment variable that lists, in order, the directories the shell searches when you type a command — and understanding it is the difference between “command not found” mystery and instant diagnosis.


What is $PATH?

When you type ls and press Enter, the shell does not know where ls lives. It searches a list of directories, in order, for an executable file named ls — and that list is $PATH:

echo $PATH
# /usr/local/bin:/usr/bin:/bin

Each entry is a directory, separated by colons (:). The shell tries the first directory, then the second, and so on, running the first match it finds. Nothing else — no magic, no registry. PATH is a plain environment variable like any other, which is why it can be read, written, and occasionally destroyed.

Why Learn $PATH?

  • It fixes the most common beginner failure. “Command not found” is almost always a PATH problem: the command exists, but the shell isn’t looking where it lives.
  • It protects you from surprises. The first matching directory wins, so a file you put in ~/bin can shadow a system command — by design, and by danger.
  • It’s how you install software “properly.” Knowing where binaries go (/usr/local/bin vs /usr/bin vs ~/.local/bin) is the difference between an install that works and one that fights you.
  • It explains weird behavior. Why does python run the wrong version? Why did sudo lose your custom tools? PATH answers all of it.

PATH is the shell’s phone book for commands. Every “command not found” is a page missing from that book — and now you know exactly which pages to check.


1. Reading PATH

See the current search order

echo $PATH
# /home/user/.local/bin:/usr/local/bin:/usr/bin:/bin

# One directory per line — much easier to read:
echo "$PATH" | tr ':' '\n'

# The full variable, with quotes:
declare -p PATH

Reading the output: the shell searches left to right. In the example above, ~/.local/bin beats /usr/local/bin, which beats /usr/bin.

See how a specific command resolves

type ls          # "ls is aliased to..." or "ls is /usr/bin/ls"
type -a ls       # every match in the search order, all of them
which ls         # /usr/bin/ls (first match only)
command -v ls    # POSIX-safe: prints the resolved path

type is the most informative: it tells you whether what you typed is an alias, a function, a builtin, or an external file — and where the file lives.


2. What’s In a Standard PATH

The exact value varies by distribution and login method, but the shape is consistent:

DirectoryContains
~/bin, ~/.local/binYour own binaries (user installs)
/usr/local/binSoftware you or scripts compiled/installed by hand
/usr/local/sbinAdmin tools installed by hand
/usr/binThe distribution’s packaged commands — most of what you use
/binEssential commands (on modern systems, a symlink to /usr/bin)
/usr/sbinSystem administration commands
/sbinEssential admin commands (also often a symlink)

Two conventions worth internalizing:

  • bin = binaries for everyone; sbin = binaries for system administration. If you’re logged in as a normal user, sbin directories are often absent from your PATH — which is why ifconfig “disappears” on some systems until you use sudo.
  • User directories come first. Distributions deliberately put ~/.local/bin and /usr/local/bin ahead of /usr/bin so that your installs can override or supplement packaged software.

Where does a command actually come from?

type -a git
# git is /usr/bin/git
# git is /usr/local/bin/git

dpkg -S /usr/bin/git        # Debian/Ubuntu: which package owns it?
rpm -qf /usr/bin/git        # Fedora/RHEL: which package owns it?
pacman -Qo /usr/bin/git     # Arch: which package owns it?

The dpkg -S / rpm -qf / pacman -Qo trio answers “what installed this?” for whatever’s in your PATH.


3. Adding to PATH

For the current session (temporary)

export PATH="$PATH:/opt/myapp/bin"      # append: system commands win
export PATH="/opt/myapp/bin:$PATH"      # prepend: your version wins

Order matters: prepending lets your directory shadow everything else; appending means existing commands still take priority. This lasts only until the terminal closes.

Permanently (survives reboots)

Add the export to your shell’s startup file — see environment variables, section 5 for which file applies to your shell:

# ~/.bashrc (bash) or ~/.zshrc (zsh), for interactive shells:
export PATH="$HOME/.local/bin:$PATH"

# /etc/profile.d/mypath.sh (system-wide, all users):
export PATH="/opt/myapp/bin:$PATH"

Then reload without logging out:

source ~/.bashrc        # or: . ~/.bashrc

The canonical user-bin directory

Most modern tools install per-user binaries into ~/.local/bin (pip’s --user installs, cargo install, manual downloads). Creating it and adding it to PATH once is a good habit:

mkdir -p ~/.local/bin
export PATH="$HOME/.local/bin:$PATH"    # then make it permanent as above

4. Removing From and Fixing PATH

Remove an entry

# Delete /opt/myapp/bin from PATH, one entry at a time:
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v '^/opt/myapp/bin$' | paste -sd:)
export PATH

(That’s why tr ':' '\n' and paste -sd: matter — they’re the standard toolkit for surgery on colon-separated lists.)

The classic disaster: unset PATH

unset PATH
ls            # ls: command not found

Nothing is broken — the shell just can’t find anything. Fix it without restarting:

export PATH=/usr/bin:/bin

Or open a new terminal. Lesson: when in doubt, edit PATH (export PATH="..."), never unset it.

The sudo trap

sudo resets PATH by default (for security — secure_path in /etc/sudoers). Your ~/.local/bin additions vanish under sudo:

sudo mytool              # command not found, even though it works as you

Fixes, in order of preference: use full paths (sudo /home/user/.local/bin/mytool), or have admins set Defaults secure_path=... in /etc/sudoers (via visudo).


5. PATH in Practice

# Debug: what will this command actually run?
type -a python3
command -v node

# Verify an install landed somewhere on the search path:
ls ~/.local/bin
echo "$PATH" | tr ':' '\n' | head -5

# Run a script by its own directory, not by PATH:
./deploy.sh                 # works regardless of PATH
bash /opt/scripts/deploy.sh # absolute path always works

# One-off PATH for a single command:
PATH="/opt/myapp/bin:$PATH" mytool --version

A healthy mental model: ./script.sh and /full/path/to/tool never touch PATH at all. PATH only matters for bare command names — which is exactly why “command not found” errors vanish when you use a path.


Common Pitfalls

1. “Command not found” but the binary exists

ls /opt/myapp/bin/mytool    # it's there
mytool                      # command not found

/opt/myapp/bin isn’t in your PATH. Add it (section 3) or run the full path. Confirm with type mytool.

2. Forgetting export

PATH="$PATH:/opt/myapp/bin"     # shell variable only
mytool                          # still not found — children don't see it

PATH is special: the shell consults its own variable, but any command you launch sees only the exported environment. Export it.

3. Spaces inside PATH entries

export PATH="$PATH:/My Tools/bin"     # breaks: "My" and "Tools/bin" become entries

Paths with spaces need escaping or quoting — better yet, avoid them in directories that go on PATH.

4. Wrong order of prepend/append

Prepending "$PATH:/new" puts your directory last — system commands still win. To make your version take priority, prepend the new directory: "/new:$PATH".

5. sudo losing your tools

As covered in section 4: sudo uses secure_path, not your PATH. Use absolute paths under sudo, or ask for secure_path to be updated.

6. Duplicating entries on every shell start

If your startup file contains export PATH="$PATH:/opt/app" and you source it repeatedly, PATH grows copies of the entry. Harmless but messy — guard with case ":$PATH:" in *":/opt/app:"*) ;; *) export PATH="$PATH:/opt/app";; esac if you care.


PageRelationship
Environment VariablesThe mechanism PATH is built on — export, startup files
AliasesAnother way to make short names do more work
Shell Scripting BasicsScripts depend on PATH to find their commands
HistoryRecover the command that broke your PATH
which / whereis / typeTools for answering “where is this command?”

$PATH Cheatsheet

CommandDoes what
echo $PATHPrint the current search path
echo "$PATH" | tr ':' '\n'One directory per line
type -a cmdShow every match for cmd in PATH order
command -v cmdPOSIX-safe: what would run for cmd
export PATH="$PATH:/new"Append a directory (system commands win)
export PATH="/new:$PATH"Prepend a directory (yours wins)
export PATH=/usr/bin:/binEmergency reset after destroying PATH
dpkg -S $(command -v cmd)Which package owns the command (Debian/Ubuntu)
rpm -qf $(command -v cmd)Which package owns the command (Fedora/RHEL)
pacman -Qo $(command -v cmd)Which package owns the command (Arch)
source ~/.bashrcReload startup files after editing PATH
~/.local/binThe standard per-user binary directory
/usr/local/binWhere hand-installed software belongs

PATH is one variable with an outsized reputation: every “command not found” is a PATH question, and every PATH question has a five-second answer once you can read the list.