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.
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.
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
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.
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
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
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.
- 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
- 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
printenvto list this, it’ll always be
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.
|Files||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|
|Processes||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|
|Networking||netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw|