Skip to main content

Add to Path in Linux

Installed a new software and want to run it from anywhere? You need to modify the PATH variable. Learn how to add to path in this quick tutorial.

Sagar Sharma

Warp Terminal

If you have been using Linux for a while, you may be familiar with "Everything is a file in Linux" and that's very true as even the directories are files.

But the question we must ask is how your system will reach the file that you want to execute. The simple answer to that question is by showing a path to the system where to look for the executable.

This is the role of a PATH variable.

But then, you'd want to know how you add the path to the $PATH environment variable.

Well, this guide will walk you through all the basics you should know related to the $PATH variable including what it is and how you add a path locally and globally.

Importance of the $PATH variable

The $PATH is an environment variable found in Linux and when you try to print the values of the $PATH variable, you will get similar results to this:

echo $PATH

You see the multiple paths separated by colon and Linux will traverse through each of them to find the executable.

For example, if you execute a command, typically, it will try to search from the /usr/local/bin directory and then will look at /usr/bin.

Some packages may require setting up the $PATH variable to make things work. For your reference, you will find the entry of the cargo package manager in the above image (where I displayed the output of $PATH variable) at the end.

In simple terms, the $PATH variable serves as a torchlight and shows where to look for the executable file you are looking for.

How to add the path to $PATH variable in Linux

You have two choices to add the path of a directory to the $PATH variable: temporary and permanent.

To add a path, you have to use the export command but there are two ways you can pull that off as it gives you the convenience of either adding the path at the beginning or at the end of the variable.

To add the path to the beginning of the variable:

If you want to add your path to the beginning of the $PATH variable, you have to append the path before the $PATH variable as shown:

export PATH=/the/file/path:$PATH

For example, I took a dummy path to show the effect:

export PATH=/show/me/first:$PATH

Now, print the variable and you'll know the effect:

echo $PATH

To add the path to the end of the variable

If you want to add the path at the end of the $PATH variable, all you have to do is append the path after the $PATH variable as shown here:

export PATH=$PATH:/the/file/path

To demonstrate this, I used a dummy path (yep, this time too):

export PATH=$PATH:/i/am/last

When I printed the value of the PATH variable, it gave me the following output:

echo $PATH
add path at the end of the $PATH variable in Linux

Did you see the difference between the two approaches?

The good thing is you can use them in both permanent and temporary methods.

Adding path temporary

The temporary method will only work for the current session which is helpful for the temporary usage.

To add the path, use the export command and append the path as shown here:

export PATH=/some/new/path:$PATH

For example, here, I added a path for apche-maven using the following command:

export PATH=$PATH:/home/sagar/maven/apache-maven-3.9.6/bin

Finally, use the echo command to verify if the path was added or not:

echo $PATH
add path to the $PATH variable temporary

Adding path permanently

If you want to add the path to the $PATH variable permanently, you have to make changes in the bashrc file.

Whenever you open a new bash shell, it uses instructions from the bashrc shell so if you write an instruction to add the path, it will apply to every shell you open.

First, open the bashrc file using the following command:

nano ~/.bashrc

Go to the end of the file in the nano editor by pressing Alt + / and add the path in the following manner:

export PATH=/some/new/path:$PATH
add the path in the $PATH variable permanently

Once done, save changes and exit from the nano editor.

To take effect from the changes you've made, source the file as shown:

source ~/.bashrc

That's it!

More on variables in bash

To change the value of the environment, you can use the envsubst command, and here's how you do it:

Replace Environment Variables Using the envsubst Command
Learn how to replace environment variables using the envsubst command. This guide will show you how with examples and tips.

Here's how to check if the variable is empty or not:

Check if Variable is Empty in Bash
Learn various ways to check if a variable is empty in bash.

I hope you will find this guide helpful.

Sagar Sharma