How to Add a Directory to PATH in Linux

By 

Updated on

6 min read

Adding a directory to the PATH environment variable in Linux

The $PATH variable tells your shell which directories to search for executable files when you type a command. Without the right directories in $PATH, the shell cannot find your programs and returns a command not found error.

This guide explains how to temporarily and permanently add a directory to $PATH in Linux, covering user-level and system-wide methods.

What is PATH in Linux

The $PATH environment variable is a colon-delimited list of directories that tells the shell where to search for executable files. Common directories in the default $PATH include /bin, /sbin, /usr/bin, /usr/local/bin, and /usr/local/sbin. All files with executable permissions stored in these directories can be run from any location.

To check the directories currently in your $PATH, use the echo or printenv command:

Terminal
echo $PATH
output
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

If two directories contain an executable with the same name, the shell runs the one found in whichever directory appears first in $PATH.

Quick Reference

For a printable quick reference, see the Bash cheatsheet .

GoalMethod
Temporary (current session)export PATH="$HOME/bin:$PATH"
Permanent for current userAdd export to ~/.bashrc and source it from your login shell file
Permanent for all usersCreate a file in /etc/profile.d/

Add a Directory to PATH Temporarily

To add a directory to $PATH for the current shell session only, use the export command:

Terminal
export PATH="$HOME/bin:$PATH"

The export command makes the updated variable available to child processes spawned from the current shell. The change is lost when the session ends or the terminal is closed.

You can now run executables stored in $HOME/bin by name, without specifying their full path.

Add a Directory to PATH Permanently

To make a PATH change permanent, add the export statement to your shell configuration files.

User-Level: ~/.bashrc and Login Shell Files

There are two shell startup files to be aware of:

  • ~/.bashrc - read for interactive non-login shells, such as most terminal emulator windows in a desktop environment.
  • ~/.profile or ~/.bash_profile - read for login shells, such as SSH sessions and TTY logins.

To cover both cases, add the export to ~/.bashrc and ensure your login shell file sources it. Open ~/.bashrc with your text editor :

Terminal
nano ~/.bashrc

Add the following line at the end of the file:

~/.bashrcsh
export PATH="$HOME/bin:$PATH"

Save the file and load the new $PATH into the current session using the source command:

Terminal
source ~/.bashrc

To confirm the directory was added, print the value of $PATH:

Terminal
echo $PATH

If your login shell is Zsh, add the same export line to ~/.zshrc instead of ~/.bashrc. For a full walkthrough, see how to install and use Zsh .

Tip

On most modern distributions, ~/.local/bin is the conventional directory for per-user executables, and tools like pip install --user and pipx place scripts there. If a command installed this way returns command not found, add ~/.local/bin to $PATH:

~/.bashrcsh
export PATH="$HOME/.local/bin:$PATH"

Many distributions add this directory automatically when it exists at login, so logging out and back in may be enough.

If you log in via SSH, ensure your existing login shell file sources ~/.bashrc so the same PATH change applies to login shells. Use ~/.bash_profile only if it already exists. Otherwise, prefer ~/.profile:

Terminal
nano ~/.profile
~/.profilesh
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

System-Wide: /etc/profile.d/

To add a directory to $PATH for all users on the system, the recommended approach is to create a new file with a .sh extension in the /etc/profile.d/ directory. Files in this directory are sourced automatically for all users at login.

For example, to add /opt/myapp/bin to the system PATH, create the file:

Terminal
sudo nano /etc/profile.d/myapp.sh

Add the following line:

/etc/profile.d/myapp.shsh
export PATH="/opt/myapp/bin:$PATH"

The change takes effect for all users on the next login.

Info
/etc/environment is another system-wide configuration file, but it is a plain key=value file parsed by PAM, not a shell script. It does not support variable expansion, so you cannot use $PATH or $HOME inside it. Use /etc/profile.d/ for system-wide PATH additions instead.

Prepend vs Append

The examples above prepend the new directory ("$HOME/bin:$PATH"), placing it at the beginning of $PATH. This gives your directory higher priority. If a directory contains an executable with the same name as a system binary, your version runs first.

To append instead, place the new directory at the end:

Terminal
export PATH="$PATH:$HOME/bin"

Appending gives lower priority to your directory. System binaries take precedence over any executable with the same name in your custom directory. For personal scripts, prepending is usually the right choice.

Remove a Directory from PATH

To permanently remove a directory from $PATH, open the configuration file where it was added and delete or comment out the export line. The change takes effect in new shell sessions. If you are unsure which startup file controls your shell type, see .bashrc vs .bash_profile .

To remove a directory from $PATH for the current session only, use sed to filter it out:

Terminal
PATH=$(echo ":$PATH:" | sed 's|:/home/user/bin:||' | sed 's/^://;s/:$//')

In the command above, we are wrapping $PATH with colons on both sides so the target directory can be matched regardless of its position, then passing the result through sed to strip the entry and clean up any leading or trailing colons.

For a directory that was added only for the current session, the simplest approach is to open a new terminal. Temporary PATH changes do not persist across sessions.

Troubleshooting

Command not found after adding to PATH
You likely forgot to reload the configuration file. Run source ~/.bashrc to apply the changes in the current terminal, or log out and back in for login shells.

PATH changes apply in the terminal but not in SSH sessions
~/.bashrc is not read directly by login shells. Ensure your login shell file, usually ~/.profile or an existing ~/.bash_profile, sources ~/.bashrc, then log out and back in.

PATH variable appears broken or commands stop working
You likely used export PATH="/your/dir" without including $PATH, which overwrites the entire variable. Open a new terminal to restore the default PATH, then correct the export line to export PATH="/your/dir:$PATH".

FAQ

How do I check if a specific directory is already in PATH?
Run echo $PATH | tr ':' '\n' to print each directory on its own line, then scan the output for the directory you are looking for.

What is the difference between ~/.bashrc and ~/.bash_profile for PATH?
~/.bashrc is read for interactive non-login shells (most terminal windows). Login shells read files such as ~/.profile or ~/.bash_profile. Put the export in ~/.bashrc and have your existing login shell file source it so the PATH change applies in all contexts.

How do I add a directory to PATH for all users?
Create a file with a .sh extension in /etc/profile.d/ containing the export statement. The file is sourced for all users at login. Do not edit /etc/environment for this purpose; it does not support variable expansion.

Does PATH order matter?
Yes. The shell searches directories in order from left to right and runs the first matching executable it finds. Directories listed earlier have higher priority.

Conclusion

The quickest way to add a directory to $PATH is export PATH="$HOME/bin:$PATH" in the current session. To make the change permanent, add that line to ~/.bashrc and have your login shell file source it when needed. For system-wide changes, drop a .sh file in /etc/profile.d/.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page