> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Add to Path in Linux
- URL: https://linuxhandbook.com/add-to-path/
- Published: 2023-12-13T06:28:40.000Z
- Updated: 2023-12-13T06:28:40.000Z
- Description: 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.
- Author: Sagar Sharma
- Tags: Tips

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
```

![](https://linuxhandbook.com/content/images/2023/12/Print-the-value-of-the-PATH-variable-in-Linux.png)

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](https://linuxhandbook.com/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
```

![](https://linuxhandbook.com/content/images/2023/12/add-path-at-the-beginning-of-the--PATH-variable-in-Linux.png)

#### 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](https://linuxhandbook.com/content/images/2023/12/add-path-at-the-end-of-the--PATH-variable-in-Linux.png)

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](https://linuxhandbook.com/content/images/2023/12/add-path-to-the--PATH-variable-temporary.png)

### 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](https://itsfoss.com/nano-beginning-end-file/?ref=linuxhandbook.com) 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](https://linuxhandbook.com/content/images/2023/12/add-the-path-in-the--PATH-variable-permanently.png)

Once done, [save changes and exit from the nano](https://itsfoss.com/nano-save-exit/?ref=linuxhandbook.com) editor.

To take effect from the changes you've made, [source the file](https://linuxhandbook.com/source-command/) 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](https://linuxhandbook.com/envsubst-command/), and here's how you do it:

[Replace Environment Variables Using the envsubst CommandLearn how to replace environment variables using the envsubst command. This guide will show you how with examples and tips.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookSagar Sharma![](https://linuxhandbook.com/content/images/2023/03/envsubst-linux-command.png)](https://linuxhandbook.com/envsubst-command/)

Here's how to [check if the variable is empty or not](https://linuxhandbook.com/check-variable-empty-bash/):

[Check if Variable is Empty in BashLearn various ways to check if a variable is empty in bash.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookSagar Sharma![](https://linuxhandbook.com/content/images/2023/06/check-if-a-variable-is-empty-or-not-bash.png)](https://linuxhandbook.com/check-variable-empty-bash/)

I hope you will find this guide helpful.