The PATH Variable: A Developer’s Guide to Command Lookup
Learn how the PATH variable tells Linux where to find your commands. Master viewing, modifying, and troubleshooting execution errors in your shell.

Previously in this course, we explored Environment Variables: A Developer’s Guide to Shell Configuration. While that lesson covered how to store generic data in the shell, this lesson focuses on one specific, critical variable: PATH. This variable is the engine behind every command you type, and understanding it is the primary way to fix "command not found" errors.
Understanding the PATH from First Principles
When you type a command like ls or grep into your terminal, the shell doesn't magically know where those programs live on your hard drive. Instead, it performs a command lookup.
The PATH variable is a colon-separated list of directories. When you execute a command, the shell scans these directories from left to right, looking for an executable file that matches the name you typed. If it finds it, it runs it. If it reaches the end of the list without a match, the shell returns the dreaded: bash: command: command not found.
Viewing Your Current PATH
Because PATH is an environment variable, you can view it using the echo command.
Bashecho $PATH
You will see an output similar to this:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Each colon (:) acts as a separator. If you have a script named deploy.sh located in /home/user/bin, and that directory is not in your PATH, you cannot simply type deploy.sh. You would have to type the full path: /home/user/bin/deploy.sh.
Adding Directories to the PATH
As developers, we often write custom scripts (like our web server automation scripts from previous lessons) that we want to run as if they were native system commands. To do this, we append our custom directory to the existing PATH.
The syntax to update the variable is:
export PATH=$PATH:/your/new/directory
Example:
Suppose you have a project directory at ~/bin where you store your custom web server management scripts. You want to make these accessible from anywhere:
Bash# Add the directory to the current session export PATH=$PATH:$HOME/bin
By putting $PATH at the beginning, you ensure that the system's default directories are searched first. By putting your directory at the end, you ensure you don't accidentally override system commands (like replacing the real ls with a custom script named ls).
Troubleshooting "Command Not Found"
When you encounter the command not found error, follow this checklist:
- Check the path: Use
which <command>to see where the shell thinks the command is. If it returns nothing, it’s not in yourPATH. - Verify file existence: Navigate to the directory and use
ls -lto ensure the file exists and has execute permissions (thexin the permission string). - Check the PATH content: Ensure the directory containing the file is actually listed in the output of
echo $PATH. - Typo check: Remember that Linux is case-sensitive.
Deploy.shis not the same asdeploy.sh.
Hands-on Exercise: Enabling Your Web Server Scripts
In our ongoing project, you have likely created scripts in your project folder to help manage your server. Let's make them executable from any directory.
- Identify the directory where your web server scripts reside (e.g.,
/home/username/project/scripts). - Add this directory to your
PATHusing theexportcommand shown above. - Verify the change by running
echo $PATHand checking if your directory appears at the end. - Navigate to your home directory (
cd ~) and try to run one of your scripts by name only.
Common Pitfalls
- Temporary vs. Persistent: The
exportcommand only affects the current terminal session. Once you close the window, the change is lost. We will cover how to make these changes permanent in the next lesson. - Overwriting the PATH: Never run
export PATH=/your/dirwithout including$PATHinside it. If you do this, you will wipe out all standard system paths, and basic commands likels,cd, andcpwill stop working until you restart your shell! - Leading vs. Trailing: If you put your directory at the start of the
PATH(e.g.,export PATH=/my/dir:$PATH), your custom scripts will take priority over system commands. Only do this if you intentionally want to shadow system utilities.
FAQ
Q: How do I know which directory a command is actually running from?
A: Use the which command. For example, which python3 will return the exact path to the executable the shell is currently using.
Q: Can I remove a directory from the PATH?
A: You cannot easily "remove" one item from the string. Instead, you usually re-export the PATH variable with a new string that omits the unwanted directory.
Q: Does the shell search recursively through subdirectories?
A: No. The PATH only lists specific directories. If your script is in a subdirectory of a folder in your PATH, the shell will not find it.
Recap
The PATH variable is a simple list of directories that the shell uses to resolve commands. By viewing it with echo $PATH and extending it with export, you gain control over how your system finds your custom development tools. Always remember to append using $PATH to avoid breaking your environment.
Up next: Configuring Shell Profiles. We will learn how to make these PATH changes permanent so you don't have to re-export them every time you open a terminal.
Work with me

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server — properly configured, hardened, and deployment-ready. No more wrestling with the command line.

Custom WordPress Plugin Development
Custom WordPress & WooCommerce plugins built to standard — by the developer behind a plugin with 5,000+ active installs and a SaaS with 10,000+ users.

