The author selected the Wikimedia Foundation to receive a donation as part of the Write for DOnations program.
After installing a command-line program, you may only be able to run it in the same directory as the program. You can run a command-line program from any directory with the help of an environment variable called PATH
.
The PATH
variable contains a list of directories the system checks before running a command. Updating the PATH
variable will enable you to run any executables found in the directories mentioned in PATH
from any directory without typing the absolute file path.
For example, instead of typing the following to run a Python program:
- /usr/bin/python3
Because the /usr/bin
directory is included in the PATH
variable, you can type this instead:
- python3
The directories are listed in priority order, so the ones that will be checked first are mentioned first.
In this tutorial, you will view the PATH
variable and update its value.
For an overview of environment variables, refer to the How To Read and Set Environmental and Shell Variables on Linux article.
PATH
VariableYou can view the PATH
variable with the following command:
- echo $PATH
An unchanged PATH
may look something like this (file paths may differ slightly depending on your system):
Output/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
Some directories are mentioned by default, and each directory in PATH
is separated with a colon :
. The system checks these directories from left to right when running a program.
When a command-line program is not installed in any of the mentioned directories, you may need to add the directory of that program to PATH
.
PATH
Environment VariableA directory can be added to PATH
in two ways: at the start or the end of a path.
Adding a directory (/the/file/path
for example) to the start of PATH
will mean it is checked first:
- export PATH=/the/file/path:$PATH
Adding a directory to the end of PATH
means it will be checked after all other directories:
- export PATH=$PATH:/the/file/path
Multiple directories can be added to PATH
at once by adding a colon :
between the directories:
- export PATH=$PATH:/the/file/path:/the/file/path2
Once the export
command is executed, you can view the PATH
variable to see the changes:
- export PATH=$PATH:/the/file/path
- echo $PATH
You will see an output like this:
Output/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/the/file/path
This method will only work for the current shell session. Once you exit the current session and start a new one, the PATH
variable will reset to its default value and no longer contain the directory you added. For the PATH
to persist across different shell sessions, it has to be stored in a file.
PATH
VariableIn this step, you will add a directory permanently in the shell configuration file, which is ~/.bashrc
if you’re using a bash shell or ~/.zshrc
if you’re using a zsh shell. This tutorial will use ~/.bashrc
as an example.
First, open the ~/.bashrc
file:
- nano ~/.bashrc
The ~/.bashrc
file will have existing data, which you will not modify. At the bottom of the file, add the export
command with your new directory:
- ...
- Adding paths to your PATH
- export PATH=$PATH:the/file/path
Use the methods described in the prior section to clarify whether you want the new directory to be checked first or last in the PATH
.
Save and close the file. The changes to the PATH
variable will be made once a new shell session is started. To apply the changes to the current session, use the source
command:
- source ~/.bashrc
You can add new directories in the future by opening this file and appending directories separated by a colon :
to the existing export
command.
The PATH
environment variable is a crucial aspect of command-line use. It enables you to run command-line programs, such as echo
and python3
, from any directory without typing the full path. In cases where adding the directory to PATH
isn’t part of the installation process, this tutorial provides the required steps. For more on environmental variables, see How To Read and Set Environmental and Shell Variables on Linux.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!