How to Add a Directory to PATH in Linux

By 

Updated on

9 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/
User-installed tools (pip --user, pipx)Add ~/.local/bin to $PATH
A downloaded binaryRun chmod +x on it, then move it into ~/.local/bin

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 .

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

Add ~/.local/bin to PATH

~/.local/bin is the conventional directory for per-user executables, and package managers put their scripts there. When pip install --user or pipx finishes successfully but the command it installed returns command not found, a missing ~/.local/bin entry is almost always the reason.

To add the directory permanently, append the export line to ~/.bashrc in one command:

Terminal
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc

Then reload the file so the change applies to the current session:

Terminal
source ~/.bashrc

Notice the single quotes around the export statement. They stop the shell from expanding $HOME and $PATH while the line is being written, so ~/.bashrc receives the literal statement instead of a frozen copy of your current $PATH.

Distribution Notes

Several distributions handle ~/.local/bin for you, so it is worth checking the default behavior before editing anything.

On Debian, Ubuntu, and Linux Mint, the default ~/.profile prepends both ~/bin and ~/.local/bin, but only when those directories already exist:

~/.profilesh
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

That test runs at login, which is why creating the directory from a running shell appears to do nothing. Log out and back in, and the directory is picked up on its own.

Current Fedora releases and RHEL 9 handle these directories in ~/.bashrc rather than ~/.bash_profile. The default block adds them at the beginning only when that prefix is not already present:

~/.bashrcsh
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]; then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH

Older releases and existing user accounts may keep this assignment in ~/.bash_profile or use a different order, so inspect both files before changing them. When the directories come before the system paths, an executable you install yourself wins over a system binary of the same name.

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.

Add a Binary or Executable to PATH

Adding a directory to $PATH only tells the shell where to look. The executable still needs execute permission, and either the file or a symbolic link to it must live in one of those directories.

First, create the standard directory for per-user executables. The -p option leaves it unchanged if it already exists:

Terminal
mkdir -p ~/.local/bin

If ~/.local/bin is not already in $PATH, add it using the steps in the previous section before continuing.

Say you downloaded a binary named mytool into ~/Downloads. Mark it executable with chmod :

Terminal
chmod +x ~/Downloads/mytool

Then move it into a directory that is already in your $PATH:

Terminal
mv ~/Downloads/mytool ~/.local/bin/

If you would rather keep the binary where it is, create a symbolic link instead. This is handy for tools that live in a project directory or a version-specific folder:

Terminal
ln -s ~/Downloads/mytool ~/.local/bin/mytool

Either way, confirm that the shell can find it:

Terminal
command -v mytool
output
/home/user/.local/bin/mytool

The full path in the output means the lookup succeeded. If nothing is printed, either the directory is still missing from $PATH or the file is not executable. The which command reports the same path and, with which -a, lists every match, which helps when two directories hold a binary with the same name.

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. Replace /home/user/bin with the directory you want to drop, spelled out in full: the sed script is single-quoted, so $HOME would not be expanded there.

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.

Why is /usr/sbin not in my PATH?
PATH defaults vary by distribution, release, and login method. Ubuntu and RHEL commonly include /sbin and /usr/sbin, while Debian login defaults may omit them for regular users. Fedora 42 began merging /usr/sbin with /usr/bin, so new installations do not need a separate entry. Check the current value with echo "$PATH". If /usr/sbin is a separate directory and is missing, add export PATH="/usr/local/sbin:/usr/sbin:/sbin:$PATH" to the appropriate shell configuration file and reload it.

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