The PATH Variable in Linux

The PATH variable tells your Linux shell where to search for executable programs when you run a command. This post explains how PATH works, how to add directories to it safely, and how to troubleshoot common PATH-related errors.

The PATH Variable in Linux

When you type a command like ls or python3 into your terminal, Linux does not magically know where to find it. Behind the scenes, your shell consults a special environment variable called PATH to locate the program you want to run. Understanding how PATH works gives you real control over your Linux environment, and it is one of those foundational concepts that keeps coming up whether you are writing scripts, installing tools, or troubleshooting why a command "isn't found."

What Is the PATH Variable?

PATH is an environment variable that holds a list of directory locations, separated by colons. When you run a command, your shell searches each of those directories in order, left to right, until it finds an executable file with that name. If it reaches the end of the list without finding anything, you get the dreaded command not found error.

To see your current PATH, run:

echo $PATH

You will see output that looks something like this:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/snap/bin

Each entry separated by a colon is a directory your shell will search. The order matters: if two directories contain an executable with the same name, the one found first in PATH wins.

How the Shell Uses PATH

When you type ls, your shell looks for a file called ls in /usr/local/sbin first, then /usr/local/bin, then /usr/sbin, and so on. You can verify exactly where a command is being found using which:

which ls
/usr/bin/ls

You can also use type to get more detail, including whether a command is a shell built-in, an alias, or an external binary:

type ls
ls is aliased to `ls --color=auto'

Adding a Directory to PATH

A common task is adding a new directory to your PATH. Say you installed a custom script to /home/yourname/scripts and want to run it from anywhere. You can temporarily add it for your current session like this:

export PATH="$HOME/scripts:$PATH"

Breaking this down:

  • export: makes the variable available to child processes, not just the current shell
  • $HOME/scripts: the new directory you are adding
  • :$PATH: appends your existing PATH so you do not lose access to standard commands

This change only lasts until you close your terminal. To make it permanent, add the same line to your shell configuration file. For Bash users, that is ~/.bashrc. Open it with a text editor and append the export line at the bottom, then reload it:

source ~/.bashrc

Common PATH Pitfalls

A few things catch beginners off guard:

  • Overwriting PATH by accident: if you run PATH="/home/user/scripts" without including $PATH, you will replace the entire variable and lose all standard commands until you restart your session
  • Adding the current directory (.) to PATH: this is a security risk on shared systems, as malicious scripts in a directory could run instead of system commands
  • Case sensitivity: PATH must be uppercase; path is a different, unrelated variable

Checking and Debugging PATH

If a command is not found, start by checking whether its directory is in your PATH. Use echo $PATH and look for the expected directory. You can also use which or whereis to locate an installed binary and compare that location against your PATH:

whereis python3
python3: /usr/bin/python3 /usr/lib/python3 /usr/share/man/man1/python3.1.gz

If /usr/bin is in your PATH and you still cannot run the command, the binary may not be installed at all, or there may be a permissions issue.

What's Next

Now that you understand how Linux finds executables, the next natural step is learning about shell configuration files in depth: ~/.bashrc, ~/.bash_profile, and /etc/environment. Knowing the difference between these files, when each one loads, and how they affect your shell environment will round out your understanding of how Linux sessions are configured from login to logout.

🔧
If you spend a lot of time working in terminal sessions debugging PATH issues and shell environments, SecureCRT is worth looking at — it offers robust session management, scripting support, and a much more comfortable experience than a basic terminal for day-to-day Linux work. SecureCRT.