Open In App

How to Insert a Directory to PATH in Linux

Last Updated : 31 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Adding a directory to the PATH in Linux allows the system to locate and execute scripts or programs stored in that directory without requiring the full path each time.

When you type a command in the Linux terminal, the shell searches for the corresponding executable file to run it. These executables, such as ls, grep, or file, are stored in specific system directories like /bin, /usr/bin, or /usr/local/bin. But how does the shell know where to look for these files? The answer lies in the $PATH variable, which tells the shell where to search for executable files.

This article explains what the $PATH variable is, why it’s important, and how you can add or modify directories in the PATH to customize your Linux environment.

What is $PATH in Linux?

The $PATH variable is an environment variable that contains a colon-separated list of directories. It specifies where the shell should look for executable files when you type a command.

To check the current directories in your $PATH, run:

echo $PATH

Example Output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

The shell searches these directories in order. If two files with the same name exist in different directories, the shell will execute the one in the directory listed first in the $PATH.

Why Add a Directory to $PATH?

Adding a directory to $PATH makes it easier to run scripts or programs stored in custom directories. For instance:

  • You may want to create a directory for your personal scripts (e.g., ~/bin) and execute them without typing the full path.
  • Some applications might install their binaries in uncommon locations not included in the default $PATH.

How to Add a Directory to $PATH in Linux?

1. Temporary Addition (For Current Session Only)

To temporarily add a directory to your $PATH, use the export command:

export PATH="$PATH:/path/to/directory"

This change lasts only for the current terminal session. Once you close the terminal, the modification is lost.

Example:

If you have a directory ~/scripts where your custom scripts are stored, run:

export PATH="$PATH:$HOME/scripts"
Screenshot-from-2024-12-30-16-08-11

You can now execute your scripts without typing the full path.

2. Permanent Addition (For All Future Sessions)

To make the change permanent, add the directory to the $PATH variable in the appropriate shell configuration file:

  • For a single user: Edit the user's shell configuration file (e.g., ~/.bashrc, ~/.bash_profile, or ~/.zshrc).
  • For all users: Edit global configuration files like /etc/environment or /etc/profile.

Steps to Add to ~/.bashrc:

  1. Open the file in a text editor:
    nano ~/.bashrc
    Screenshot-from-2024-12-30-16-16-47
  2. Add the following line at the end of the file:
    export PATH="$PATH:/path/to/directory"
    Screenshot-from-2024-12-30-16-17-47
  3. Save the file and apply the changes:
    source ~/.bashrc

Verify the Change: Check if the directory has been added by running:

echo $PATH
Screenshot-from-2024-12-30-16-19-59

How to Remove a Directory from $PATH?

Over time, you may need to remove a directory from the PATH for various reasons, such as cleaning up unused entries or resolving conflicts. While there's no single command to directly remove a directory from PATH, several methods can help you achieve this effectively:

Method 1: Exit the Terminal or Reboot the System

If the directory was added to the PATH temporarily during the current session, it will automatically be removed once the terminal is closed or the system is rebooted. This approach is useful if you made a temporary change using the export command, such as:

export PATH="$PATH:/path/to/temp-directory"

Simply close the terminal or reboot to revert the PATH variable to its previous state.

Method 2: Edit Configuration Files for Permanent Changes

If the directory export string was added to the .bashrc or .profile file, remove it using the same method. Open the file in a text editor, navigate to the end of the file, and remove the directory. Save the changes and reload the configuration file using the source command. This ensures the changes take effect immediately without restarting the system.

Method 3: String Replacement in the Current Session

For a current session-only removal, use string replacement to exclude the directory from PATH:

export PATH=${PATH//:/path/to/remove}
Screenshot-from-2024-12-31-11-20-29
  • ${PATH//:/path/to/remove} replaces occurrences of /path/to/remove in the current PATH value.
  • The updated PATH is applied temporarily and reverts when the terminal is closed.

Method 4: Using a One-Liner with tr, grep, and paste

To remove a directory from PATH programmatically, use a combination of commands:

export PATH="$(echo $PATH | tr ':' '\n' | grep -v '/path/to/remove' | paste -s -d ':')"

This method is ideal for scripting and ensures the change affects only the current session.

Conclusion

Inserting a directory into the PATH in Linux allows the system to locate and execute scripts or programs stored in that directory without specifying the full path. This can be done temporarily using the export command or permanently by modifying configuration files like ~/.bashrc.

Managing the PATH effectively—by adding or removing directories—ensures a streamlined workflow, easy access to custom scripts, and a clean, efficient environment for seamless command execution.


Article Tags :

Similar Reads