How to Set the PATH Variable in Linux (2 Ways)

The PATH Variable in Linux is an important component that contains the list of directories containing the commands that can be input using the command line interface. It is similar to Environmental Variables on Windows. Most modern-day apps with a GUI interface do not require you to specify a PATH variable. You can modify the PATH variable to tell the Linux machine in which directories to look for commands to execute. In this guide, let’s look at how to set the PATH variable in Linux.

Set PATH Variable in Linux by Editing ~/.bashrc

One of the easiest ways to set a default PATH variable permanently on Linux is by editing the .bashrc file. For those unaware, it is a script file that executes whenever a user logs in to their session and it helps connect user commands by directing them to the default directories stored in it. Here’s how to add a new PATH variable in .bashrc.

1. Launch the terminal and open the .bashrc file using your favorite text editor in Linux. We will be using the nano editor and don’t forget sudo.

sudo nano ~/.bashrc
Bashrc command

2. Go to the end of the file and start typing the default path that you want to add after adding export=$PATH:.We’re choosing the path to the downloads folder as an example, but you will have to choose the path that leads to your program. The end result should look like this.

export=$PATH:/home/abubakarmohammed/Downloads

Note: The path and PATH different and should not be confused with each other. path refers to the address of a file/directory in the file system, whereas PATH is an environment variable.

add export path in with default path in Terminal

3. Once done editing, press the key combination Ctrl + O to write the data, and press enter to save it. And finally, press Ctrl + X to exit.

4. Finally, use the following command to save the PATH changes.

source ~/.bashrc

5. To ensure your PATH variable is saved and updated, use the following command to check if it’s there. You will see the path that you added in the previous step in the output.

echo $PATH
Echo PATH command

Set the PATH variable by using the export Variable

There are times when you may not need to set a variable permanently. For example, if you have got a file that should only be executed once, it might not be a great idea to add it permanently on the PATH. For those instances, you may want to use the export variable.

1. Launch the terminal and type the following command to know your current default PATH address.

echo $PATH

2. Copy the path that you want to add temporarily and paste it after “export.” After exporting, you can check the PATH again using the echo command.

export PATH="/[directory_name]:$PATH"
export path temporarily

3. This temporary PATH will stay as long as you don’t terminate the terminal session.

comment Comments 0
Leave a Reply