Key Takeaways
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.
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 50 environment variables on each of them.
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.
How to Show an Environment Variable’s Value
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.
echo $HOME
echo $USER
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.
How to List Environment Variables Using printenv
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.
printenv -0
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.
printenv
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
.
printenv | 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:
printenv | grep DISPLAY
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 using the GNOME desktop environment.
- BASHOPTS: The list of command line options that were used when bash was launched.
- BASH_VERSION: The version of bash.
- COLUMNS: The width of the terminal in columns.
- DIRSTACK: The stack of directories for use with the
pushd
andpopd
commands. - HISTFILESIZE: The maximum number of lines of command history that can be written to the history file.
- HISTSIZE: The maximum number of lines of command history allowed to be stored in memory. If you go past this number, previously remembered commands are overwritten in memory. When you close your terminal window, the command history is written to the history file.
- HOME: The current user’s home directory.
- HOSTNAME: The name of the computer.
- IFS: The internal field separator that is used to parse user input. The default value is a space.
- LANG: The current language and localization settings, including character encoding.
- LS_COLORS: This defines the codes that are used to add color to the output from ls.
- MAIL: The path to the current user’s Linux mailbox.
- OLDPWD: The previous working directory.
- PS1: The primary command prompt definition. This defines what the prompt in your terminal window looks like.
- PATH: A colon-separated list of directories that are searched, in order, for a matching command or application when you type a command into the shell.
- PWD: The current working directory.
- SHELL: The name of your default shell.
- TERM: The type of terminal that is emulated when you run a shell.
- UID: The user identifier of the current user.
- USER: The current user.
- _: The most recently executed command. If you use
printenv
to list this, it’ll always beprintenv
.
Environmental Inspections
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.