In Linux, the PATH environment variable determines which directories the system searches for executable files. Adding a directory to the PATH allows you to run scripts or programs located in that directory without specifying the full path each time.
This guide will show you how to temporarily and permanently add a directory to your PATH.
What Is the PATH Environment Variable?
The PATH environment variable is a colon-separated list of directories that the shell searches for executable files when you type a command. When you run a command in the terminal, the system checks these directories to see if the command exists there.
You can view the current value of the PATH by running:
echo $PATH
Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
In this example, directories like /usr/bin and /usr/local/bin are part of the PATH. Any executable placed in these directories can be run directly from the terminal without specifying its full path.
Why Add a Directory to PATH?
There are many practical scenarios where adding a directory to your PATH is helpful. For instance, you may have custom scripts stored in /opt/my_program/bin. Adding this directory to your PATH allows you to run those scripts from anywhere in the terminal without typing the full path.
Suppose you have scripts or programs in /opt/my_program/bin. By adding this directory to your PATH, you can run:
my_program_script
Instead of typing the full path like so:
/opt/my_program/bin/my_program_script
Temporarily Add a Directory to PATH
Use the export command to add a directory to the PATH for the current terminal session. This change will only last until the terminal is closed.
1. Use the export command to add the directory:
export PATH=$PATH:/opt/my_program/bin
2. Verify the directory has been added:
echo $PATH
Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/my_program/bin
3. You can now run any executable in /opt/my_program/bin without specifying the full path. For example:
my_program_script
Note that this change is temporary and will reset once you close the terminal session.
Permanently Add a Directory to PATH
To permanently add a directory to the PATH, you must modify one of your shell’s configuration files. Common files include:
- .bashrc: Runs for interactive non-login shells.
- .bash_profile: Runs for login shells.
- .zshrc: If you’re using the zsh shell.
Let’s go through the steps to add a directory to the PATH permanently:
1. Open your shell configuration file. In this case, we’ll modify .bashrc for bash users.
nano ~/.bashrc
2. Add the following line at the end of the file:
export PATH=$PATH:/opt/my_program/bin
3. Save and exit the file.
4. Apply the changes by running:
source ~/.bashrc
5. Verify the changes by rechecking the PATH:
echo $PATH
Now, the directory /opt/my_program/bin is permanently added to your PATH, and you can run scripts or executables in that directory from any terminal session.
Conclusion
Adding directories to your PATH in Linux makes it easier to run executables or scripts from any location in the terminal. Whether you’re adding a directory temporarily or permanently, it’s a simple yet powerful way to streamline your workflow. Now, you can run your custom programs or scripts without having to type out full paths every time. Try to add PATH to the dedicated server hosting from Atlantic.Net!