Running commands in Linux requires the system to locate executable files. Programs like ls, grep, and find are stored in standard directories. However, scripts or programs stored elsewhere need the system to know their location. This is where the $PATH variable becomes essential. Adding a directory to $PATH allows you to execute programs from any location without typing full paths.
What is the PATH Variable in Linux
The $PATH variable contains a colon-separated list of directories. When you type a command, the shell searches these directories in order to find the executable file.
View your current $PATH with:
$ echo $PATH
Example output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The shell searches directories from left to right. If two executables share the same name but exist in different directories, the shell runs the one found first.
$PATH.
| Component | Purpose |
|---|---|
$PATH |
Environment variable containing executable directories |
Colon : |
Separator between directory paths |
echo $PATH |
Command to display current PATH contents |
How to Check Your Current PATH in Linux
Before adding directories to $PATH, verify what directories are already included.
Use the echo command:
$ echo $PATH
Alternatively, use printenv:
$ printenv PATH
Both commands display the same information. The output shows all directories where the system looks for executables.
To check where a specific command is located:
$ which ls
Output:
/usr/bin/ls
This confirms that ls resides in /usr/bin, which is included in $PATH.
Add a Directory to PATH Temporarily in Linux
Temporary changes affect only the current terminal session. Once you close the terminal, the modification disappears.
Add a directory with the export command:
$ export PATH="$HOME/bin:$PATH"
This prepends $HOME/bin to $PATH. Programs in this directory now run without specifying the full path.
Verify the change:
$ echo $PATH
The new directory appears at the beginning of the list.
$PATH to prioritize it over existing directories.
Why Temporary Changes Matter
Temporary modifications are useful for testing scripts or programs before making permanent changes. They help avoid conflicts with system executables during development.
Add a Directory to PATH Permanently in Linux
Permanent changes persist across sessions. Store the export command in shell configuration files.
For Single User (Bash)
Edit ~/.bashrc for Bash users:
$ nano ~/.bashrc
Add this line at the end:
export PATH="$HOME/bin:$PATH"
Save the file and apply changes:
$ source ~/.bashrc
The directory is now permanently added to $PATH for this user.
For Single User (Zsh)
Zsh users should edit ~/.zshrc:
$ nano ~/.zshrc
Add the same export line:
export PATH="$HOME/bin:$PATH"
Apply changes:
$ source ~/.zshrc
For All Users (System-Wide)
To add a directory for all users, edit /etc/environment:
$ sudo nano /etc/environment
Modify the PATH line to include your directory:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/custom/bin"
Log out and log back in for changes to take effect.
| Scope | Configuration File |
|---|---|
| Bash (single user) | ~/.bashrc |
| Zsh (single user) | ~/.zshrc |
| All users | /etc/environment or /etc/profile |
Remove a Directory from PATH in Linux
Removing a directory from $PATH requires editing the configuration file where it was added.
Remove Temporarily
Use sed to remove a directory for the current session:
$ PATH=$(echo "$PATH" | sed -e 's/:/home/username/bin$//')
Replace /home/username/bin with the directory to remove. This change lasts only until you close the terminal.
Remove Permanently
Edit the configuration file where the directory was added. Open ~/.bashrc, ~/.zshrc, or /etc/environment and delete the corresponding export line.
Apply changes:
$ source ~/.bashrc
The directory no longer appears in $PATH for new sessions.
FAQs
Use export PATH="$HOME/bin:$PATH" for temporary changes. For permanent changes, add the command to ~/.bashrc or ~/.zshrc and run source to apply.
The shell executes the first match found. Directories are searched left to right in $PATH. Place higher-priority directories at the beginning.
Edit /etc/environment or /etc/profile for system-wide changes. Modify the PATH line to include your directory and log out for changes to take effect.
Run echo $PATH | grep "/your/directory" to search for a specific directory. If found, it appears in the output. Otherwise, the directory is not included.
Yes. Use PATH=$(echo "$PATH" | sed -e 's/:/path/to/remove$//') to remove a directory for the current session. Close the terminal to reset.

