How is the path environment variable managed in Linux/Ubuntu/Debian?

Many users, beginners and advanced are sometimes confused on how the PATH environment variable is handled on Linux. Why there are so many files .bash_profile, .bashrc,.profile , .bash_login ?
In this short article, we will try to clear up this confusion and will also explain how to add a path to PATH on Linux as well as provide a glimpse into the different files involved and the way they are invoked.

PATH environment variable

We all know that when you type in a command in the terminal on Linux, we are instructing the kernel to run the corresponding program. For example common commands like cd, echo, rm are simply short programs that are usually located inside /usr/bin.

Other executable programs can be found inside /usr/local/bin or /usr/sbin…In general, these type of programs can reside practically anywhere on the system, i.e. they do not have a specific directory.

When a command is invoked, the kernel does not need to search in every folder its corresponding program since it relies on the $PATH environment variable which informs the kernel about the whereabouts of the correct program.

Read: Environment Variables in Python

In order to be able to execute some programs that may have been installed in other uncommon directories on your machine, you could easily indicate their location by adding a directory to your $PATH. To find out what is inside your $PATH, run the command below :

echo $PATH

Ubuntu environment variables

You will most likely see the directories that were mentioned earlier along with some others. You could notice that the entries are separated by colons.

How a path is added to PATH ?

The answer to how a new path is added to the PATH environment variable can be the following for instance : using the files

PATH=$PATH:~/opt/bin or PATH=~/opt/bin:$PATH         [set environment variable linux | linux add to path]

This however will depend on whether the user wants to add ~/opt/bin at the end (to be searched after all other folders or directories if a program with the same name exists in multiple folders) or at the start (to be searched rather before all other folders).

Read : How to fix high memory usage in Linux

Multiple entries can be added at the same time. PATH=$PATH:~/opt/bin:~/opt/node/bin or other variations on the ordering will work just fine.

For example, to add directory /data/bashscripts to the start of the $PATH environment variable, proceed as follows:

PATH=/data/bashscripts:$PATH.           [how to set path in ubuntu]

To add it to the end, use the following command:

PATH=$PATH:/data/bashscripts             [add to path linux]

Once you add the path to the $PATH and reboot, your change is gone. This is a standard behavior (see next section).

If the PATH gets constructed by different components, this might lead to duplicate entries. Some distributions actually put ~/bin in the PATH automatically if it exists.

How to permanently add a directory to $PATH

Insert the line to modify PATH in the file ~/.profile or in ~/.bash_profile (in case you do not care about shells different from bash) if it exists. You would need to source this in order for it to persist:

source ~/.profile

In general and depending on the final goal, there are various ways that allow users to permanently add a directory to $PATH.

The variable values are stored usually in either a shell script that is run at the start of the system or user session or in a list of assignments. You must use a specific shell syntax and set or export commands in case of the shell script .

Read : How to keep Ubuntu clean

Scope : User’s session

A – ~/.profile, ~/.<shell>_profile, ~/.<shell>_login Shell script. Only programs executed from terminal will be able to read these. A suitable option for systems that are shell-only.
For instance the content of ~/.profile applies session wide like programs that you start once the user logs in ( not graphical programs) and environment variable definitions. If you log in via a GUI, you do not get a login shell that reads the file ~/.profile automatically. Depending on the window manager, the graphical login program or desktop environment you execute later on and how your Linux system configured these applications, the file ~/.profile may or may not be read.

The .bash_profile is used to initialize the shell upon login and to setup variables as well as aliases. Instead of ~/.profile, ~/.bash_profile can be used.

Shells that are in login mode will be able to use these files.

B~/.xprofile. Shell script. When the user logs into X Window System, this is executed.
By default .xprofile does not exist in your home folder unless you create one. This is
Ideal for extending PATH with common values like ~/bin or ~/go/bin or specifying user defined OWNPATH or NODE_HOME. The instructions for making a .xprofile file can be found here. The defined variables can be ‘seen’ by every X application. Do not use the syntax of your own user shell but rather use POSIX shell syntax, since the file is included by other scripts.

C – ~/.<shell>rc. Shell script. This is a rather bad choice since it is specific to a single shell. Used in non login mode.
The content of ~/.bashrc for instance applies to bash only, like for instance function definitions, aliases , prompt settings and shell options. It is the settings file of interactive instances of bash. There are some recommendations to either insert the definitions of the environment variables in ~/.bashrc or launch login shells in terminals. These are not good ideas since the environment variables will only be set in programs invoked via the terminal not in programs launched directly with a menu or an icon or keyboard shortcut

Read: what is the difference between non login and login non interactive and interactive shell sessions in linux ubuntu debian

Scope: System

A – /etc/environment.d var wpcf7 = {"apiSettings":{"root":"https:\/\/\/wp-json\/contact-form-7\/v1","namespace":"contact-form-7\/v1"},"cached":"1"};