Unlocking the full potential of your Linux system involves understanding the importance of the $PATH variable, which serves as a roadmap for your operating system to locate and execute commands swiftly and effortlessly. However, if you find yourself repeatedly setting the
$PATH every time you log in or reboot, it is time to learn how to permanently configure it.
In this comprehensive guide, we will walk you through the step-by-step process of setting the
$PATH variable in Linux, ensuring that your customizations persist across sessions.
What is a $PATH Variable in Linux
$PATH variable is a fundamental part of the Linux operating system, acting as a reference to locate executable files. It consists of a list of directories separated by colons
(:), with the system searching for commands in these directories when they are executed. Understanding the importance of the $PATH variable is crucial to effectively manage your command-line experience.
There are multiple approaches to permanently setting the
$PATH variable in Linux and the two most common methods involve:
- Editing Shell Configuration Files
- Utilizing Environment-Specific Files
Each method has its advantages and disadvantages, so choose the one that best suits your needs. The PATH variable can also be set on a per-user basis or system-wide for all user accounts.
Check $PATH Variables in Linux
You can see your
$PATH variables by running the following echo command:
$ echo $PATH
Add a Directory to PATH Variable Linux
To temporarily add a directory, for example,
/opt/sysadmin/scripts to your path, you can run the following command:
$ PATH=$PATH:/opt/sysadmin/scripts $ echo $PATH
One way to permanently set the
$PATH variable is by modifying shell configuration files, such as
.bash_profile. These files are executed each time you start a new shell session.
$ nano ~/.bashrc OR $ vim ~/.bashrc
Locate the line that begins with
"export PATH=" and modify it to include the directories you want to add to the
$PATH variable. Separate each directory with a colon
If you are unable to find this line, you can add it manually inside the file at the bottom of the page. As an example, I am adding the /opt/sysadmin/scripts directory path to my PATH using the command given below:
export PATH="$PATH: /opt/sysadmin/scripts"
To apply the changes, either open a new terminal or run the following command:
$ source ~/.bashrc
Note: The above method only works for user accounts.
Permanently Set $PATH for All Users
To permanently set system PATH for all users on the system, append the following lines at the end of the
/etc/profile file. On some Linux distros, you can also use the
/etc/bash.bashrc file (if it exists) but it should be sourced in /etc/profile for changes in it to work.
PATH="$PATH:/opt/sysadmin/scripts" export $PATH OR $export PATH="$PATH:/opt/sysadmin/scripts"
Alternatively, instead of making changes directly in the
/etc/profile (which is not recommended), you can create a script (ending with the
.sh extension) under the directory
/etc/profile.d (if it exists) and make the changes in that script.
$ sudo vim /etc/profile.d/set_system_path.sh
Append the following line in it:
Next, source the
/etc/bash.bashrc (depending on the one you used), for the changes to take effect.
$ source ~/.bashrc OR $ source /etc/profile OR $ source /etc/bash.bashrc
Then confirm that your PATH has been set correctly:
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/sysadmin/scripts:/opt/sysadmin/scripts
Set PATH Variable in /etc/environment File
Another approach to permanently setting the
$PATH a variable is by using environment-specific files. Two common files used for this purpose are /etc/environment and /etc/profile.d/.
$ sudo nano /etc/environment
Next, locate the line that begins with
"PATH=" and add the directories you want to include, separated by colons
To apply the changes, reboot your system or run the following command:
$ source /etc/environment
After making modifications to the
$PATH variable, it’s crucial to verify that the changes have taken effect.
$ echo $PATH
This will display the updated
$PATH variable, including the directories you added. Ensure that the desired directories are now part of the
To make the most of your Linux system, it’s a must for you to understand the
$PATH variable that acts like a map and helps your computer find and run commands quickly.
If you are tired of setting
$PATH every time you log in or restart your computer, better to set it permanently using the step-by-step guidelines presented in the above article.
If you have managed to do this process, you will have a better command-line experience on your Linux system.