How Can You Add a Directory to PATH in Linux

How Can You Add a Directory to PATH in Linux






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.

Note: The current working directory is not searched unless explicitly included in $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.

Tip: Place the new directory at the start of $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.

Warning: Editing system-wide configuration files affects all users. Verify changes carefully to avoid breaking executable access.
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.

Note: Closing and reopening the terminal also removes temporary PATH changes.

FAQs