How to list Environment variables in Linux

In this article, we cover how to list Environment variables in Linux. These store critical information about a shell session and help us interact with the system better. Let’s take the PATH environment variable to understand it.

PATH environment variable contains the path of directories where the shell would look for the executable files for the commands we enter. If it wasn’t there then, the shell would search the entire Linux distribution for the executable. This definitely slows everything down. So, we have the PATH environment variable. The shell now knows where to look for executables. And, gets us the result in no time.

How to list Environment variables in Linux

We will be covering two command-line tools that can be used to list Environment variables:

  1. printenv and,
  2. set.

We start with printenv first. It basically displays the value stored in the variable. For instance,

printenv PATH

But, if we don’t provide the variable name, then it just displays the names of all the environment variables it can (mainly Global Environment variables) and their stored values.

But, certain variables like BASH_VERSION, we know won’t get listed here. Try

printenv | grep -i BASH_VERSION

The set built-in command displays the shell variable names and stored values. It also covers shell functions as well. Just use it without options,

set | more

We use more along with it as the list of such variables is long, and it is better to display them on one screen at a time.

Try checking for the BASH_VERSION variable here.

set | grep -i BASH_VERSION

In conclusion, we have covered here how to list environment variables in Linux here.

Similar Posts