A variable is a given name to a location or an object where data is stored for future reference. Whereas the environment variable is used for storing dynamic values that can store a list of directories of a Linux system that you use for executing applications. In Linux, the environment variable is mentioned with a $ prefix and written in upper case PATH. It stores all the directories that the bash search for when we execute a command on the terminal shell in Linux. A fun fact about the $PATH is, you can add your own directory and create your own terminal command if you know how to set $PATH in Linux.
Set $PATH in Linux
If you’ve been using Linux for a very notable period, you might have wondered that how does the terminal shell works, how do the commands get the location and access system to install a program or execute a script. Here comes the $PATH in Linux! If you want to make your Linux journey enjoyable, you can ask your computer where the ‘sudo’ is, which executes all the root commands?
I’m sure you’re already familiar with the which
and whereis
command since you’re here, reading about the $PATH. In this post, we will see how to set $PATH in Linux.
Step 1: Check Current PATH
Knowing the location and work mechanism of the current $PATH is a vital thing if you’re a complete newbie in the PATH, Bashrc area on Linux. You may execute the following which
command to see where is the sudo located.
# which sudo
You may now also check current $PATH variables through the following Echo command. The Echo command will print the exact location of the $PATH. Here you can see that the current location of $PATH for my Linux is /usr/local/games:/snap/bin
.
$ echo $PATH
Step 2: Add a Temp $PATH
Here, we will now declare a temp directory to $PATH variable to the path /opt/sysadmin/scripts
. You can choose your desired directory where you want to set the $PATH on your Linux system.
$ PATH=$PATH:/opt/sysadmin/scripts
Then print the new temporary $PATH location through the Echo command given below.
$ echo $PATH
Step 3: Set $PATH Permanently in Linux
Here, we will see two different methods to set the $PATH on a Linux system. The first method will guide us on editing the exiting PATH, and the other one will let us know how to create a different script to set the $PATH without actually overwriting the existing one.
Method 1: Edit the Current $PATH
Since we have already gone through the notion, location, and location of the $PATH on Linux, we can now set a directory of $PATH permanently on our system. You can run any of the following bash scripts to set the $PATH.
~/.bash_profile ~/.bashrc
To edit the bash file, we will use the traditional Gedit script editor tool with root privileges. You can use Vim or Nano if you’re comfortable with it.
$ sudo gedit ~/.bashrc
Now, when the script opens, please add any of the following script lines at the bottom of the file. Then save and exit the file. As you can see that the line we are adding is actually defining the location of the $PATH on your system.
PATH="$PATH:/opt/sysadmin/scripts" $export PATH
The below export command will do the same if you find the above command is not working for you.
$export PATH="$PATH:/opt/sysadmin/scripts"
If your Linux system has a multi-user arrangement, you can set the current $PATH for all users as their default $PATH as well through the following script lines.
PATH="$PATH:/opt/sysadmin/scripts" export $PATH
We can also run the below command if we face issues with the above commands.
$export PATH="$PATH:/opt/sysadmin/scripts"
Method 2: Create a New Script To Set $PATH in Linux
In this method, we will create a new script file to set up the $PATH inside the etc
directory on Linux. This method is safe because we don’t need to edit any running bashrc
script, so the chances of messing up the system are less. However, you can run the following command on the terminal shell to make a new bash profile.
sudo vim /etc/profile.d/set_system_path.sh
Now, when the script opens, please add the following line at the bottom of the file. Then save and close the file. The following script line will set the $PATH location on the system. Please be careful to input your exact directory where you want to set the$ PATH.
export PATH="$PATH:/opt/sysadmin/scripts"
Now we will have to reload the source .bashrc file on the system. We can either reload the source .bashrc file or the profile that we have created under the etc directory. Any of the following commands will do the work.
$ source ~/.bashrc $ source /etc/profile $ source /etc/bash.bashrc
Now, finally, to ensure the $PATH directory location, we can execute the Echo command.
$ echo $PATH
Insights!
Knowing the functionality and setting the $PATH is not mandatory to use Linux. You can use Linux smoothly even not checking the PATH location for a single time. However, in the entire post, we have seen how to set $PATH on Linux. With proper guidelines, we can also remove the current $PATH both temporarily and permanently.
Please share it with your friends and the Linux community if you find this post useful and informative. You can also write down your opinions regarding this post in the comment section.