BASH Programming

How to Export a Path in .Bashrc

When a command is launched on Linux, the operating system looks in the environment variable which is known as PATH for executable files. When we employ these paths, we can run the commands without having to provide a specific path.

What Are Environmental Variables in Linux?

In Linux, the environment variables are a collection of configurable named variables that were already saved on the device that is run on the terminal of Linux-based devices. The environment variable contains a name, and it has a specified value in it. We can alter the default path in Linux using the environment variable. For instance, the environment variable may keep track of the default search engine, the location of executable commands, or the keyboard configuration.

Here are the different techniques to view the default path. You can also add the path by yourself:

Technique 1: Default “Path” in Linux

If you want to display the default path in Linux, we write the following command in the Linux terminal:

linux@linux-VirtualBox:~$ echo $Path

The following is the default path of the Linux device. The file paths may vary somewhat based on your Linux system. As you verify in the following output, the colon “:” is used to separate each “Path” directory in the terminal, as well as some directories which are listed by default in the output. When a command is started, the system scans these directories from left to right.

If we write the “echo $Path” command and we didn’t get the path of the directory, we have to add the directory path. Here is an example: when we enter the command to view the path, we didn’t view any path as you can see in the following illustration:

Technique 2: Adding the Inode to a Temporary Path Variable

To create a temporary Path in Linux, we write the following command in the terminal:

linux@linux-VirtualBox:~$export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/bin:

/usr/games:/usr/local/games:/snap/bin

Now, we can access the path by writing “echo” along with the path_name.

linux@linux-VirtualBox:~$ echo $PATH

Here is the desired output of the command:

This method of creation only exists until we end the existing terminal which means that it is a temporary session. The PATH variable reverts to its default settings whenever you quit the existing activity and start another one, and the recently inserted inode won’t be present. An inode must be used to hold the PATH so that it is persistent throughout several terminal sessions. We use another method where it holds the inode permanently in the terminal session.

Technique 3: Permanently Adding the Inode at the End to a Path Variable

This method adds the Path permanently in the terminal configuration file. To add permanently, we use the “bashrc” command. Here is the complete statement:

linux@linux-VirtualBox:~$ nano ~/.bashrc

After hitting enter, we get the following output:

To exit from the bashrc terminal, there are hints to exit the bashrc terminal at the bottom of the terminal. After coming back to the main terminal, write the “export PATH”. Then, assign the path of the inode of your choice in the Linux terminal. The following is the complete command to give the path of the inode:

linux@linux-VirtualBox:~$ export PATH=$Path:/home/linux

After writing the previous command, wait so that the changes are made after entering the path of your choice in the Linux terminal. Then, save the bashrc file and close it.

Once a new terminal session is launched, the PATH variable is modified. Now, just employ the “source” statement to implement the modifications to the existing session. Use the following command to implement the source command to the bashrc file:

linux@linux-VirtualBox:~$ source ~/.bashrc

In the future, if you want to add a new inode in the bashrc file, use the colon “:” to add the directories to the existing “export” statement.

After importing the updated bashrc file, check whether the Linux existing address is saved in the Linux device default path or not. The following is the command to confirm the updated address/path. First, write “echo” and then write the “$PATH” to access the updated address.

linux@linux-VirtualBox:~$ echo $PATH

Here is the desired output of the previously-implemented commands in the Linux terminal. As you see in the following output, we have now an updated address in the bashrc file because the new inode address is added to the previous path which is “/home/linux”.

Here is another command in Linux to display the current existing address in the bashrc file in the Linux terminal:

linux@linux-VirtualBox:~$ printenv PATH

The following is the output of the “printenv” command which displays the same output because it works the same as “echo” in the Linux terminal:

Technique 4: Adding the Inode at the Start to a Path Variable in Linux Permanently

If you want to add a particular path at the start of the existing path in the bashrc file in Linux, you have to follow the instruction that is written in the following. This method is the same as we previously did. The only difference is to add the path before accessing the “$PATh” in the bashrc file in Linux.

linux@linux-VirtualBox:~$ export PATH=/home/linux:$Path

After writing the previous command in the bashrc file, we now implement these changes in the file.Use the “source” command to update the bashrc file in Linux like this:

linux@linux-VirtualBox:~$ source ~/.bashrc

After updating the file, we now want to check whether the modifications are done or not in the bashrc file in the Linux terminal. Write the following written command:

linux@linux-VirtualBox:~$ echo $PATH

The following is the output of the modification which we did in the bashrc file in the Linux terminal:

As you see in the output, the new path is added at the start of the previous path that is permanently saved in the bashrc file.

Conclusion

In this tutorial, we learned about one of the environment variables in Linux called “Path.” We learned how to get the default path in the terminal and what to do to add the path if it is not already there. With a thorough explanation, we learned how to permanently add the path at the beginning and at the end of the existing address in the bashrc file.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.