1. Overview

An environment variable is a dynamic object that defines a location to store some value. We can change the behavior of the system and software using an environment variable. Environment variables are very important in computer programming. They help developers to write flexible programs.

There are multiple ways to list or display an environment variable in Linux. We can use the env, printenv, declare, or set command to list all variables in the system.

In this tutorial, we’ll explain how to list environment variables in Linux.

2. Using the printenv Command

The printenv command-line utility displays the values of environment variables in the current shell.

We can specify one or more variable names on the command line to print only those specific variables. Or, if we run the command without arguments, it will display all environment variables of the current shell.

For example, we can use the printenv command followed by HOME to display the value of the HOME environment variable:

$ printenv HOME
/root

In addition, we can specify multiple environment variables with the printenv command to display the values of all the specified environment variables:

Let’s display the values of the HOME and SHELL environment variables:

$ printenv HOME SHELL
/root
/bin/bash

We can see a list of all of the environment variables using the printenv command without any arguments:

$ printenv
XDG_VTNR=7
XDG_SESSION_ID=c1
SHELL=/bin/bash
TERM=xterm
USER=root
SUDO_USER=vyom
SUDO_UID=1000
USERNAME=root
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
...

3. Using the env Command

env is another shell command we can use to print a list of environment variables and their values. Similarly, we can use the env command to launch the correct interpreter in shell scripts.

We can run the env command without any arguments to display a list of all environment variables:

$ env
XDG_VTNR=7
XDG_SESSION_ID=c1
SHELL=/bin/bash
TERM=xterm
USER=root
SUDO_USER=vyom
SUDO_UID=1000
USERNAME=root
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
...

We can use the grep command to filter the value of the specific variable we’re interested in:

$ env | grep USERNAME
USERNAME=root

4. Using the set Command

set is yet another command-line utility for listing the names and values of each shell variable. Although the set command has other uses, we can display the names and values of all shell variables in the current shell simply by running it without any options or arguments:

$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="3" [2]="11" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
...

5. Using the declare Command

declare is another built-in command used to declare a shell variable and display its values.

For example, let’s run the declare command without any option to print a list of all shell variables in the system:

$ declare
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="3" [2]="11" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
...

6. Using the echo Command

echo is also used to display values of the shell variable in Linux.

For example, let’s run the echo command to display the value of the $HOSTNAME variable:

$ echo $HOSTNAME
ubuntupc 

7. Conclusion

In this short article, we explained how to list environment variables using different Linux commands.