Understanding the PATH Variable in Linux
The Linux operating system requires users to have a sound knowledge of path definition which includes both absolute
and relative
paths. The absolute path is a file path relative to the root directory while the relative path is a file path relative to the current working directory. One setback of running scripts in the terminal is having to memorize the file path of the file when you can just run a script from anywhere in your terminal with just the name of the file. The PATH
variable is used for this purpose. In this article, we will discuss the PATH
variable, why it is needed, and how to use this variable effectively.
The PATH Variable in Linux
The PATH
variable in Linux is an environment variable that tells the shell which directory to search for executable files. Not all executable programs exist in this directory. To see our environment variable we just need to run the following command.
printenv | grep PATH
All commands in Linux are scripts stored in a particular folder e.g. /bin
folder shown above. The PATH
variable enables you to run the command anywhere in your OS. Because to run a file in Linux, you have to be positioned in the directory where that file is present. So stacking the path location in the PATH
environment variable gives the user the privilege of running the script anywhere in the Operating System (OS). Providing the path to a file could be burdensome.
Let’s see an illustration below:
- Create a directory with the name
scripts
in any directory of your choice.
mkdir scripts
2. Navigate into the directory you just created with the following commands.
cd scripts
3. Create a script with the name script1
in the directory and also write the following script in the file.
vi script1
#!/bin/bash
echo "This is my script"
4. Give executable permission to the file using the chmod
command.
chmod +x script1
5. Run the script in the present directory with the following commands.
./script1
You can see that it works perfectly.
6. Move a step back outside your present directory and try to run the script.
cd .. && ./script1
You can that the program gives us an error and it doesn’t work. This is because we are not positioned in the directory where the file is present, so the program does not see the file and gives a No such file or directory
error.
7. For you to run the scripts successfully you have to provide the relative path of the file like so:
scripts/script1
You can see that it works fine now, even when we are not positioned inside the directory that has the file in it.
You can view all paths in the PATH
variable with the following commands.
printenv | grep PATH
to input a script to the path variable we would run the following command:
export PATH=$PATH:/home/dolzy/scripts
You can now see that our path is now included in the PATH
variable.
Now we would test this, by moving out of the directory that has our script and running our script there. We would run our script in the home directory which would normally give an error when we run it.
We can see that our script now runs even without being in the scripts
folder that contains our script.
Now our script works perfectly but we still have a problem because our scripts run temporarily in this instance or process. If we open another process on the terminal and try to run our script from the home directory, the script does not run. Let us try it out in our terminal.
This is because our script path is no longer in the PATH
variable in this process.
This begs the question, how can we permanently add the path of our script?
Adding a Directory Permanently to the PATH Variable
To effect this change permanently, we need to add the directory to the hidden file in our home directory called the .bashrc
file. This can be viewed by running the following command in the terminal.
ls -la
We would open the .bashrc
file and add our path to the list of directories in the file.
Now we would open another process in our terminal and run the following command and then check the PATH
variable if our path has been added to the variable.
source ~/.bashrc
We would this command to check our PATH variables for the directories:
printenv | grep PATH
Now that our directories have been permanently added to the PATH variable, let us run our script from the home directory and see if it works.
Our script successfully runs in the home directory as before.
Conclusion
The PATH variable in Linux is very useful as it saves users the burden and stress of running scripts by necessarily knowing the path definition of the file. Saving the file directory to the PATH
variable automatically gives the user the freedom to run the file from any point in the terminal.
Thank you for reading. You can reach out and connect with me on X.