Linux Commands

Show PATH of Environment Variables

An environment variable is a key:value pair in the form of key=value. The value itself can be a number, a location, text, or any other random string. The environment variables shape the system and define various attributes. For example, the HOME variable is used to set the current user’s home folder, while the SHELL variable contains information about the current user’s shell path. Although there are a large number of pre-defined environment variables, one can always create new ones or modify existing ones. This means that any environment variable can be shown/seen, edited, saved, and deleted. And though environment variables exist on every system, we will be focusing on the Linux system. In particular, in this tutorial, we will be learning about showing the PATH of the environment variables.

Environment Variables

In order to print out all of the environment variables, we type:

env

Untitled5

The latter will print out all the environment variables and their respective values.

Alternatively, we can use:

printenv

Untitled

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:

SHELL=/bin/bash
PWD=/home/kalyani
HOME=/home/kalyani
TERM=xterm-256color
USER=kalyani
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

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

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=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/
usr/local/games:/snap/bin

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:

$ which cat

/usr/bin/cat

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.

PRINTENV

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.

printenv PATH

Untitled2

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:

printenv path

The latter returns absolutely nothing. This means no value is associated with the variable path (in lower case letters).

Untitled4

ECHO

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.

echo $PATH

Untitled3

You can show the path individually using:

echo $PATH | sed ‘s/:/\n/g’

Untitled4

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:

Untitled

What this means is that everything here is case-sensitive.

Modifying PATH

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:

Temporarily:

export PATH=$PATH:/path/to/dir

export PATH=/path/to/dir:$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:

export PATH=$PATH:/home/kalyani

echo $PATH

I would get:

Untitled

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.

Happy Coding!

About the author

Kalyani Rajalingham

I'm a linux and code lover.