Summary

To list environment variables on Linux, use the printenv command. That will show you all environment variables, but you can get searchable results by piping it into the less command. Alternatively, use grep to check specific environment variables.

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.

Using echo to see the values stored in environment variables

What are Linux Environment Variables?

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. 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.

Our various test computers have an average of 50environment variableson each of them.

The unreadable output from printenv when the -0 option is used

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 yourBash shell command historycache. You can edit the value of the$HISTSIZEenvironment variable inyour “.bashrc” fileto 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.

The standard output from printenv, with one environmental variable per line.

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.

How to Show an Environment Variable’s Value

You can useechoto 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 theenvand theprintenvcommands.

The output from printenv in the less file viewer

Theprintenvcommand is the official way to do it. The command was writtenspecifically for this purpose. Theenvcommand has analtogether different purpose.

envis 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 invokeenvwith no command line parameters, its default action is to list the environment variables.

Using grep to filter the results from printenv

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 usingprintenvto check environment variables in our examples.

How to List Environment Variables Using printenv

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

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

The effect of the-0option 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-0option. 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 variablesintoless.

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 canusegrepto 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:

Some Common Environment Variables

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 usingthe GNOME desktop environment.

Environmental Inspections

To list all of your environment variables, useprintenv. Pipe the output throughgrepto filter the results, and useechoto print the value of a specific, known environment variable.

tar·pv·cat·tac·chmod·grep·diff·sed·ar·man·pushd·popd·fsck·testdisk·seq·fd·pandoc·cd·$PATH·awk·join·jq·fold·uniq·journalctl·tail·stat·ls·fstab·echo·less·chgrp·chown·rev·look·strings·type·rename·zip·unzip·mount·umount·install·fdisk·mkfs·rm·rmdir·rsync·df·gpg·vi·nano·mkdir·du·ln·patch·convert·rclone·shred·srm·scp·gzip·chattr·cut·find·umask·wc·tr

alias·screen·top·nice·renice·progress·strace·systemd·tmux·chsh·history·at·batch·free·which·dmesg·chfn·usermod·ps·chroot·xargs·tty·pinky·lsof·vmstat·timeout·wall·yes·kill·sleep·sudo·su·time·groupadd·usermod·groups·lshw·shutdown·reboot·halt·poweroff·passwd·lscpu·crontab·date·bg·fg·pidof·nohup·pmap

netstat·ping·traceroute·ip·ss·whois·fail2ban·bmon·dig·finger·nmap·ftp·curl·wget·who·whoami·w·iptables·ssh-keygen·ufw·arping·firewalld