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
- Open a terminal: Open a terminal on your Linux machine.
- Use the export command: Type the following command and press Enter:
export PATH=$PATH:/path/to/directoryReplace
/path/to/directorywith the actual path you want to add to the$PATHvariable.
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
- Open a text editor: Open a text editor, such as
nanoorvim, and edit the shell configuration file for your Linux distribution.- For Bash shell, the configuration file is usually located at
~/.bashrcor~/.bash_profile. - For Zsh shell, the configuration file is usually located at
~/.zshrc.
- For Bash shell, the configuration file is usually located at
- Add the following line: Add the following line to the end of the file:
export PATH=$PATH:/path/to/directoryReplace
/path/to/directorywith the actual path you want to add to the$PATHvariable. - Save and close the file: Save the file and close the text editor.
- Restart your shell: Restart your shell by typing
source ~/.bashrcorsource ~/.zshrcand press Enter.
Tips and Variations
- Use the
:character: When adding a path to the$PATHvariable, 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_PATHvariable: If you want to preserve the existing$PATHvariable, you can use theOLD_PATHvariable to store the original value and then modify the$PATHvariable 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
$PATHvariable permanently, you can use a Bash alias to add the new path. For example:echo "alias mypath='PATH=$PATH:/path/to/directory'" >> ~/.bashrcThen, 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.
