banner

Blog

Sep 04, 2023

How to List Environment Variables on Linux

On Linux, environment variables hold important values and settings. Scripts, applications, and shells read these values, often to configure themselves, or to control their behavior. Here are several ways to show environment variables in your terminal, and how to interpret the list as well.

All About Environment VariablesHow to Show an Environment Variable's ValueHow to List Environment Variables Using printenvSome Common Environment VariablesEnvironmental Inspections

Our various test computers have an average of 50 environment variables on each of them. An environment variable, like any other variable, is a combination of a name and a value. The name is unique, set when the variable is created, and it last for the lifetime of the environment variable.

Variables hold values for us. When a process needs to know what the value is, it looks up the variable by name in a list, and reads the value from it. Although variable names cannot be changed, their values can be.

You won't often change system environment variables, but you can if you need to. For example, you might like to increase the size of your Bash shell command history cache. You can edit the value of the $HISTSIZE environment variable in your ".bashrc" file to set a new upper limit for the number of remembered commands.

That's neat and convenient, but it isn't something you’ll be doing often. Environment variables tend to be left at their defaults or they’re changed once and then forgotten about. They’re not something you’ll tinker with often.

Nonetheless, it is worthwhile knowing how to check the environment variables that are defined and in use on your computer. Printing the environment variables to a terminal window lets you see their values, and shows you what aspects of your Linux experience are governed by these background values.

RELATED: How to Set Environment Variables in Bash on Linux

You can use echo to see the value stored in an environment variable. To do that you’ll need to know the name of the environment variable in advance.

There are two methods commonly used to show the names and values of all the environment variables on Linux. They are the env and the printenv commands.

The printenv command is the official way to do it. The command was written specifically for this purpose. The env command has an altogether different purpose.

env is used to run an application with temporary, user-specified, values for environment variables. These override the real stored values, and allow the application to run in a modified environment. If you invoke env with no command line parameters, its default action is to list the environment variables.

We may as well use the tool designed for the job, rather than depend on the side-effect of a tool that has been invoked incorrectly, so we’ll be using printenv to check environment variables in our examples.

RELATED: How to Pass Environment Variables to Docker Containers

The printenv command lets you show environment variables and is very straightforward in practice. It has very few options. You can use the --version option to find out the release number of the version on your computer, and you can use the --help command to see a short description of these two and one other command line option.

The other option is the -0 (null terminator) option. Usually, printenv lists the environment variables one per line, by adding a newline character to the end of each line. The -0 option replaces that newline character with a null byte. You would use this option if you were piping the output into another application that didn't need the newline characters.

The effect of the -0 option in a terminal window is to cram the output together into an impenetrable wall of text.

It's practically impossible to make sense of the environment variables listed in this format. It’ll be a rare occurrence if you ever need to use the -0 option. Let's drop it, and try again.

The output is printed with one environment variable per line. By convention, environment variable names always use uppercase characters. Immediately after the variable name is an equals sign "=", followed by the value that the environment variable is set to.

There's still a lot of output, so you might find it easier to pipe the environment variables into less .

This lets you scroll through the list of environment variables, and to search the list as well.

If you know something about the environment variable you’re interested in, you can use grep to find the likely candidates. Suppose you know there's an environment variable that has the word "display" in it. We can search the list like this:

RELATED: How to Work with Variables in Bash

The default environment variables on different Linux computers are subject to the preferences of the maintainers of the various distributions, desktop environments, and shells. So when you’re checking environment variables on your Linux device, you may see varying results.

Here are some of the more common environment variables you’re likely to find on a Linux computer using the GNOME desktop environment.

To list all of your environment variables, use printenv. Pipe the output through grep to filter the results, and use echo to print the value of a specific, known environment variable.

RELATED: Best Linux Laptops for Developers and Enthusiasts

Table of Contents RELATED RELATED: How to Set Environment Variables in Bash on Linux RELATED: How to Pass Environment Variables to Docker Containers RELATED: How to Work with Variables in Bash BASHOPTS BASH_VERSION COLUMNS DIRSTACK HISTFILESIZE HISTSIZE HOME HOSTNAME IFS LANG LS_COLORS MAIL OLDPWD PS1 PATH PWD SHELL TERM UID USER _ · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · RELATED: Best Linux Laptops for Developers and Enthusiasts
SHARE