In order to print out all of the environment variables, we type:
The latter will print out all the environment variables and their respective values.
Alternatively, we can use:
The printenv command is used to print out each and every environment variable. That is the key:value pairs. Some of the environment variables on my system are:
The first thing to notice is a key:value pair. Of all these environment variables, one that we give specific attention to is the PATH environment variable.
PATH is an environment variable that allows the system to specifically find various executables by pointing them to the right directory. On Ubuntu, PATH would look something like this (this is on my system):
PATH here is the key, and to the right of the PATH variable is its value. By convention, each path is separated by a colon (:). And separated by a colon are a large number of directories.
So when you enter a command, let’s take the command “cat” as an example; the shell will search each one of these directories one after another from left to right. If it is found in one of them, then it executes it. However, please note that it will search in the exact order that it is listed in. This means it won’t search /snap/bin first; it will search it last. Further, when it has searched the /usr/local/sbin directory, it won’t randomly pick another one to search but rather search the /usr/local/bin directory.
If we were to type:
We notice that the cat command is found in /usr/bin, and as such when the system gets to /usr/bin, it will find the cat command, execute it, and terminate the search. This further means that if there were two versions of the “cat” command, the one that appears in the earlier directory would be executed, and the second one would be ignored. If the command is not found in any of the directories listed in the PATH environment variable, then a “command not found” error will be issued.
On a Linux system, there are a number of ways of displaying the PATH information.
We can obviously display the path information using the printenv command. In order to print out the PATH information using the printenv command, you need to type printenv and then the name of the environment variable that you would like to display. In our case, it’d be PATH.
You can add any of the environment variables of your choice after the printenv command, and it will print it out for you.
Please note here that the word PATH has to be in all-capitals. Suppose that I write the following:
The latter returns absolutely nothing. This means no value is associated with the variable path (in lower case letters).
Alternatively, the other common way of printing out the PATH environment variable is by using the echo command. The echo command is used to print out or echo out the value. Because PATH is a variable, in order to print it, we must add a dollar sign in front of it. Please also remember to keep all letters in the capital.
You can show the path individually using:
SED is used in this case to re-format the output by taking each path and placing them on a single line. In other words, substitute the colon with a new line.
Further, if we were to write the word PATH in lower case letters, we’d get the following:
What this means is that everything here is case-sensitive.
The PATH variable can easily be modified as well. We can add to it – prepend or append. What’s more? We can make temporary changes or permanent changes.
There are 3 ways to modify PATH:
To temporarily change the PATH, you can simply type the previous code into a terminal. However, this will be restricted to a single session.
Permanently (for the current user):
Edit the .bashrc file by adding export PATH=$PATH:/path/to/dir to it, and updating the the .bashrc file.
System-wide (for all users):
Modify the /etc/environment file. This is not recommended.
Once you have modified the PATH variable, you can again check the new PATH variable using the printenv command or the echo command.
So let’s try the temporary PATH change. Suppose that I type the following into my terminal:
I would get:
Please notice how the PATH is added to the end of the search directories.
Environment variables are a set of key:value pairs. They exist on every system, and on the Linux system, they can be viewed, edited, saved, and deleted. PATH, in particular, is an environment variable used to display the path to search for executables. When a command is issued, the system looks at the PATH environment variable to find its location. If found, the command works; otherwise, a “command not found” error is issued. Further, the PATH environment variable can be viewed using two distinct commands – the printenv command or the echo command. The PATH variable can also be modified prior to viewing in three distinct ways – temporarily, permanently, or system-wide.