Skip to main content

PATH

PATH is an environment variable holding a colon-separated list of directories the shell searches, in order, to find the program you type. The first match is the one that runs.

Understanding the Linux PATH Variable

What Is PATH and How Does the Shell Use It?

When you type node, the shell does not know where it lives. It walks through each directory in PATH, left to right, and runs the first executable named node. If none matches, you see command not found. If two directories each contain the same name, the earlier one silently wins.

◈ DIAGRAM
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
You type: node server.js
/usr/local/sbin not here
/usr/local/bin found node -> runs /usr/local/bin/node
(remaining directories are never checked)

How to View, Find, and Change PATH

Bash
echo $PATH
which node # first match
type -a python # every match, in order
# Prepend (your tools win) or append (fallback) for this session
export PATH="/opt/mytools/bin:$PATH"
export PATH="$PATH:/opt/mytools/bin"
# Make it permanent for your user
echo 'export PATH="/opt/mytools/bin:$PATH"' >> ~/.bashrc
# System-wide for all users
echo 'export PATH="/opt/mytools/bin:$PATH"' | sudo tee /etc/profile.d/mytools.sh

Why Scripts Work Interactively but Fail in cron or systemd

Different execution contexts load different environments. ~/.bashrc is read only by interactive shells, so cron jobs, systemd services, and many CI steps do not see your additions. They often get a short PATH such as /usr/bin:/bin.

Bash
# Inside an automation script, set PATH explicitly
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Troubleshooting PATH

Symptom Command What to Check
command not found which cmd Not in PATH
Wrong version runs type -a cmd Earlier directory wins
Fails in cron env > /tmp/cron.txt Minimal PATH
Fails in CI echo $PATH in the job Different runner PATH
Common Mistake

Editing ~/.bashrc and expecting cron or a systemd service to pick it up. Set PATH inside the script or the unit file.

Security

Never put . or a world-writable directory early in PATH. An attacker could plant a fake ls there and have it run in place of the real one.

Remember

Order matters. Prepending gives your directory priority, appending makes it a fallback.

Frequently Asked Questions

Why does PATH order matter?

The shell runs the first match, so a version manager's Python placed ahead of `/usr/bin` will override the system Python silently. Two people with differently ordered PATHs can run different binaries for the same command.

How do I find which executable a command will run?

Use `type -a command` to list every match in PATH order, or `which command` for the first one.

Why does PATH keep growing with duplicates?

Several startup files (`.bashrc`, `.profile`) may each append the same directory. Guard additions with a check, or keep PATH edits in one place.

Found a mistake, an outdated step or a broken link?