Hello, friends. In this post, you are going to know a trick that is for beginners, but that comes as a reminder for the more experienced ones. Today, you will know how to add a directory to the PATH.
What is the PATH variable?
The PATH variable stores the path to the directories where you should look for binaries when you run a command. Therefore, it is possible to have a list of binaries in a directory and to add them to the PATH to execute it via terminal.
The detail is that many times the binaries are in a place that is not in the PATH as the binaries that are installed in PIP or other package managers.
So let’s get started.
How to display the contents of PATH
Before we get started, I’m going to show you how to display the contents of the PATH variable. This way you will be able to know which are those directories that are part of it.
echo $PATH
You will have an output screen like this
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin:/usr/games:/usr/local/games:/snap/bin
There you will see the directories separated by :
.
Adding a directory to the PATH temporarily
The first option is to add a directory to the PATH temporarily. So, that when you restart the session, these changes will disappear. This is a good option when you want to test something or use a binary only once.
To achieve this, just run this command:
export PATH=$PATH:[directory]
Where you will have to replace [directory]
with the absolute path of your binary directory that you would like to add to the PATH.
Adding a directory to the PATH permanently
If you want the changes to be permanent, then you will have to modify the .bashrc
file with some text editor.
nano ~/.bashrc
And at the end of the file, add this line:
export PATH=$PATH:[directory]
Save the changes and close the editor. Now the changes will be permanent.
Conclusion
Thanks to this post, you learned this useful trick, so you can use it from the terminal. Of course, this is focused on binaries and even scripts.