How to add path to path variable in Linux?

How to Add Path to Path Variable in Linux: A Step-by-Step Guide

What is the $PATH variable in Linux?

The $PATH variable in Linux is an environment variable that specifies the directories where the system looks for executable files. When you enter a command in the terminal, Linux searches for the executable file in the directories listed in the $PATH variable. By default, the $PATH variable typically includes the following directories:

  • /bin/
  • /usr/bin/
  • /usr/local/bin/
  • /usr/local/sbin/
  • /sbin/

Why would you want to add a path to the $PATH variable?

You might want to add a path to the $PATH variable for several reasons:

  • To make it easier to run executables located in a specific directory
  • To prevent the need to specify the full path to an executable file every time you want to run it
  • To make it easier to use GUI applications that require the system to know where to find the executable files

How to add a path to the $PATH variable in Linux

Here are the steps to add a path to the $PATH variable in Linux:

Method 1: Using the export command

  1. Open a terminal: Open a terminal on your Linux machine.
  2. Use the export command: Type the following command and press Enter:
    export PATH=$PATH:/path/to/directory

    Replace /path/to/directory with the actual path you want to add to the $PATH variable.

Note: The export command only affects the current shell session. To make the change persistent, you’ll need to add it to your shell configuration file.

Method 2: Edit the shell configuration file

  1. Open a text editor: Open a text editor, such as nano or vim, and edit the shell configuration file for your Linux distribution.

    • For Bash shell, the configuration file is usually located at ~/.bashrc or ~/.bash_profile.
    • For Zsh shell, the configuration file is usually located at ~/.zshrc.
  2. Add the following line: Add the following line to the end of the file:
    export PATH=$PATH:/path/to/directory

    Replace /path/to/directory with the actual path you want to add to the $PATH variable.

  3. Save and close the file: Save the file and close the text editor.
  4. Restart your shell: Restart your shell by typing source ~/.bashrc or source ~/.zshrc and press Enter.

Tips and Variations

  • Use the : character: When adding a path to the $PATH variable, use the : character to separate the new path from the existing paths. This is important, as it tells the system to look for the new path in addition to the existing paths.
  • Use the OLD_PATH variable: If you want to preserve the existing $PATH variable, you can use the OLD_PATH variable to store the original value and then modify the $PATH variable accordingly. For example:
    OLD_PATH=$PATH
    export PATH=$OLD_PATH:/path/to/directory
  • Use a Bash alias: If you don’t want to modify the $PATH variable permanently, you can use a Bash alias to add the new path. For example:
    echo "alias mypath='PATH=$PATH:/path/to/directory'" >> ~/.bashrc

    Then, you can use the alias in your terminal to run the executables in the new path.

Troubleshooting Tips

  • Check the path existence: Make sure the path you’re trying to add exists and is accessible.
  • Check the shell configuration file: Verify that the shell configuration file is correct and syntax is correct.
  • Restart your shell: If you’ve made changes to the shell configuration file, restart your shell to apply the changes.

By following these steps, you can add a path to the $PATH variable in Linux and make it easier to run executables located in that directory. Remember to use the : character to separate the new path from the existing paths, and consider using a shell alias or the OLD_PATH variable if you want to preserve the existing $PATH variable.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top