How to add a directory to PATH in Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

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]
How to add a directory to PATH in Linux
How to add a directory to PATH in Linux

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.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook