PATH variable in Linux

When you run the program from terminal or a script, you usually only write the name of the program file. However, Linux is designed so that the executable and associated files programs are distributed to various specialized catalogs. For example, libraries are installed in /lib or /usr/lib, configuration files in /etc, executables in /sbin/, /usr/bin or /bin.

These locations several. Where the operating system knows where to find the desired software or a component? Everything is simple — it uses the PATH variable. This variable allows to significantly reduce the length of the typed commands in the terminal or in a script without having to specify the full path to the required files. In this article, we will understand why you need a Linux PATH variable, and add to its value the names of your custom catalogs.

PATH variable in Linux

In order to see the contents of the PATH variable in Linux, run the command in the terminal:

echo $PATH

The screen will display the list of folders, separated by colons. The search algorithm path to the required program when it starts is quite simple. Initially, the OS looks for executable file with the specified name in the current folder. If it finds one, starts the execution, if not, checks the directories listed in the PATH variable, in the established order there. Thus, adding your own folders to the contents of this variable, you add new locations of executable and related files.

In order to add a new path to the PATH variable, you can use the command export. For example, let’s add to the value of the PATH variable the directory/opt/local/bin. In order not to overwrite the existing value of the PATH variable new, you need to add (append) the new value to the existing one, without forgetting about the delimiter-colon:

export PATH=$PATH:/opt/local/bin

We can now verify that the PATH variable contains also the name of this, we added folders:

echo $PATH

You already know how in Linux to add the name of the required folder in the PATH variable, but there is one problem — after restarting the computer or open a new terminal session, all changes will be lost, your PATH variable will have the same value as before. In order to avoid this, you need to fix the new current value of the PATH variable in the system configuration file.

On Ubuntu the PATH is contained in /etc/environment file, and some other distributions, it can also be found in the /etc/profile file. You can open the file /etc/environment and manually add the desired value:

sudo vi /etc/environment


Alternatively. The contents of the file .bashrc runs every time you start the Bash shell. If you add to the end of the file and then click export, then each loaded shell will automatically add the name of the required folder in the PATH variable but only for the current user:

vi ~/.bashrc

export PATH=$PATH:/opt/local/bin


In this article we considered the question about why we need the PATH environment variable in Linux and how to add value to its new search paths for executable and related files. As you can see, all is simple enough. So you can add as many folders to search and store executable files as you want.


(Visited 11 times, 1 visits today)