The export PATH command is used to set the PATH environment variable in Linux and other Unix-like operating systems. The PATH variable contains a list of directories that the system searches for executable files when a user types a command. By modifying the PATH, users can control the locations that are searched and the executable files that can be run.
What is the PATH variable?
The PATH environment variable is a system variable that contains a list of directories separated by colons. For example:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
When a user types a command in the terminal or shell, the system will search through the directories in the PATH to locate the executable file for that command. So if a user types “ls”, the system will look for the “ls” executable in each of the directories in the PATH.
The PATH variable enables the shell to find executables in a standard set of directories so users don’t have to type out full path names for every command. This makes using the terminal and running programs much easier.
Common directories in PATH
Some common directories that are typically included in the PATH variable are:
- /bin – Basic binary executable files
- /usr/bin – More user binary files
- /usr/local/bin – User installed binary files
- /usr/local/games – User installed games
When a directory is added to the PATH, any executable files within that directory can be easily run by typing the command name. Any directories not in the PATH would require typing the full path to the executable.
Viewing the current PATH
To view the current value of the PATH variable, use the ‘echo’ command:
echo $PATH
This will print out the PATH directories separated by colons. For example:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
You can also use the ‘env’ command to display all environment variables including PATH:
env | grep PATH
Modifying the PATH
The PATH can be modified to add or remove directories. This allows control over the directories searched for executables.
Temporarily modify PATH
To temporarily modify the PATH for a single terminal session, use the export command:
export PATH=$PATH:/my/new/dir
This will add /my/new/dir to the end of the current PATH. Any executables in this directory can now be run. The change is only valid for the current terminal session.
Permanently modify PATH
To permanently modify the PATH for all future terminal sessions and reboots, add the export command to your shell profile file such as ~/.bash_profile or ~/.profile:
export PATH=$PATH:/my/new/dir
After updating the profile, source the file or open a new terminal to utilize the new PATH.
Adding directories to PATH
Common directories that are added to the PATH include:
- User installed binary directories like /usr/local/bin
- Directories containing scripts or executables for specific projects
- Locations with utility or tool binaries like Python or Go
For example, to add the /opt/bin directory to the PATH:
export PATH=$PATH:/opt/bin
Now any executables in /opt/bin can be run by name. The updated PATH includes the new directory.
Removing directories from PATH
To remove a directory from the PATH, remake the PATH without including that directory:
export PATH=$(echo $PATH | sed -e 's/:/\n/g' | grep -v "/dir/to/remove" | paste -sd :)
This uses some text manipulation utilities to split the PATH on colons, grep to filter out the directory to remove, then paste to join back together with colons.
For example, to remove /usr/games:
export PATH=$(echo $PATH | sed -e 's/:/\n/g' | grep -v "/usr/games" | paste -sd :)
Common uses of export PATH
Some common reasons to modify the PATH include:
- Add a user installed binary directory like /usr/local/bin
- Add a project directory containing scripts to PATH
- Prioritize certain versions of binaries by putting them earlier in PATH
- Remove outdated or insecure directories from PATH
This enables isolation of projects, control over version priority, and customization of available commands.
Project directories and scripts
Adding a project directory to PATH makes scripts in that project available globally. No need to type full path names.
For example:
export PATH=$PATH:/home/user/projects/myapp/scripts
Now any scripts in /home/user/projects/myapp/scripts can be run from any directory.
Prioritizing binary versions
Binary executables with the same name can exist in multiple PATH locations. The directory order in PATH determines precedence.
By putting a directory with a preferred version earlier in PATH, that version will be detected and used over other versions.
For example, to prioritize /usr/local/bin over the system /usr/bin:
export PATH=/usr/local/bin:$PATH
Now /usr/local/bin is searched first when looking for executables.
Removing insecure directories
Some directories in the default PATH may be outdated, contain old versions of binaries, or be less secure. These can be removed.
For example, to remove /usr/games from PATH for better security:
export PATH=$(echo $PATH | sed -e 's/:/\n/g' | grep -v "/usr/games" | paste -sd :)
Exporting PATH in profile files
To make PATH changes persistent across sessions, add the export command to your profile file.
Most shells load the following profile files each time a new session is started:
Shell | Profile File |
---|---|
Bash | ~/.bash_profile, ~/.profile, or ~/.bashrc |
ZSH | ~/.zshrc |
CSH | ~/.cshrc or ~/.login |
To add a directory to your PATH, add the export command to the profile file:
export PATH=$PATH:/my/new/dir
After updating the file, source it or open a new terminal to use the updated PATH.
Troubleshooting export PATH issues
Some common issues when modifying PATH and ways to troubleshoot them include:
Command not found after adding PATH directory
- Double check the directory contains the executable
- Verify PATH was exported correctly
- Source profile file or open a new terminal
Command found in wrong version
- Check order of directories in PATH
- Prioritize preferred version earlier in PATH
Updated PATH works in terminal but not other programs
- Only updated PATH for a single session
- Need to export PATH in profile file for persistence
Best practices for PATH management
Some tips for effectively managing your PATH include:
- Keep PATH centralized in profile files instead of .bashrc
- Limit editing PATH to append rather than prepend
- Avoid extremely long PATH variables
- Use full paths for cron jobs and scripts requiring consistent environments
- Restructure directories to avoid PATH modifications when possible
Following best practices avoids confusion and potential issues when modifying PATH.
Conclusion
The export PATH command allows control over the directories searched for executables in Linux and Unix. Modifying PATH enables easy access to project scripts, prioritization of binary versions, and removal of outdated directories. When updating PATH, changes can be made temporarily or persistently by adding export commands to profile files. With proper management, PATH can be customized to streamline development workflows.