How to permanently add directory to PATH in Linux?


The PATH is very important environment variable and knowing how to work with it, can be very useful. In this article, I'm going to demonstrate how to add your directory permanently into PATH. 

What is PATH?

Every time you run a command, it is the PATH environment variable which is being used to determine where to look for command. One common location is the /bin directory. It's a directory that contains some of the most commonly used commands such as pwd, ls, cp, mkdir, touch, dd, df, du, tail, less, more, and etc. 

You can display your PATH using the echo command. Here's the command.

# echo $PATH

Therefore the PATH variable is very useful because it allows the command to be executed without knowing or specifying the full path to the command. For this article, I'm going to show you how to add myapp directory into PATH which contains my bash script? Let's go.

Adding /home/amar/myapp directory into PATH

Inside my /home directory, I have created myapp directory which contain the following bash script that I have wrote. 

Once this script is executed, the standard output should be as follow.

What I want now is to add this directory into PATH variable so I can easily execute this script without passing the full path to it like so:

# /home/amar/myapp/./ 

The above path to my script is quite long. But PATH allows us to execute this script without passing the path to it. First we need to append the /home/amar/myapp directory to PATH variable. Here's the command.

# PATH="/home/amar/myapp:$PATH"

As you can see on above image, my /home/amar/myapp directory is now appended into PATH variable. But this is only temporary and when I close the terminal, this directory will be gone. So, to add it permanently into PATH, we need to edit the .bashrc profile file. 

In case you wondering what is bashrc file? Well, the bashrc file is just a script that is executed every time a new terminal session is started in interactive mode. For editing this file, you need be super user for changes to take effect. Here's the command.

# vim  /home/amar/.bashrc 

This is how the .bashrc file looks like when you open it. I have open it for editing via vim editor, but you can use any other text editor that you are comfortable with. So to add our directory /home/amar/myapp permanently into PATH, at the end of a .bashrc file, write the following command.

# export PATH="/home/amar/myapp:$PATH"

You should note that every single entry into PATH is separated by (:) semicolon sign. Ok, now let's take a look at out PATH variable now.

As you can see, there is /home/amar/myapp directory inside PATH variable which we have permanently added. Now, let's try to run the following script without passing the full path to it.

There you go. All I had to do is to enter the name of the script and script runs perfectly without passing the full path to it's directory. 


This was a very useful article on how you can add your own directory into PATH; as you have see, sometimes if you wish to run your own scripts without passing the full path to it's directory, just add it to the PATH variable. But, please be careful what you are doing. If you override the PATH content by mistake, your system would become unstable, so take your time, read this articles as well as other articles and how to guides before you start playing with the PATH variable. Till next time. 


1 komentar:

  1. This is great! One adjustment though. The edits to .bashrc do not take effect until you restart the terminal. For any terminal that was already open, you will have to manually add the directory to $PATH.