How to Print Environment Variables in Linux

Environment variables are specific to certain environments. A generic answer, right?

But really, those are the variables that are just specific to your current system environment such as the currently logged-in user will be stored inside the "USER" variable.

Still confused? no worries. I will walk you through a brief understanding of environment variables and then jump to various ways to print them.

What are Environment Variables?

Nope, it's not related to your Desktop Environment.

One of the most basic environment variables that you encounter on a daily basis is Hostname. So have you ever wondered why it's always written in all caps?

Because most of the environment variables are pre-defined by the system and are global variables so you'll encounter them often written in all caps.

So why use environment variables in the first place?

Suppose you are a programmer and your code needs to have access to your database key which should never be shared in public.

So what options do you have while sharing the entire code on Git? Wrap the database key to the environment variable.

This way you can set instructions on Git as "if you want to make this code work, interchange this variable with your database key."

Of course, this was the one way of using environment variables. So let's have a look at some common environment variables in Linux.

Environment Variable Description
HOME Shows home directory for the current user
HOSTNAME Contains Hostname of your system
UID Stores unique ID of the user
SHELL Shows the path to the shell which is being used currently
BASH_VERSION Contains version currently used bash instance
HISTFILE Path of the file where the history of commands is saved
TERM Shows the type of log-in terminal you're using
PATH Shows path of commands and directories are separated by columns

There are various ways through which you can print environment variables in Linux and I'll try to cover each of them. So let's start with the easiest one.

Using printenev Command

The printenv utility is used to print environment variables for the current shell.

Suppose I want to print the value of USERNAME using printenv so my command will be as follows:

printenv USERNAME
Using printenv to print USERNAME

Similarly, you can also use the printenv utility to print more than one environment variable by separating them with <space>.

So how about printing the value of HOME and USERNAME at once using printenv?

printenv HOME USERNAME

First, it'll print the home directory of the currently logged-in user and in the second line, the hostname will be there.

Using printenv to print HOME and USERNAME

But what if you are looking for a way by which you can print each environment variable available in the current shell?

You can easily accomplish this by executing the printenv command without any options or arguments whatsoever.

printenv
Utilizing the printenv command to print each environment variable available in the current shell

Using echo Command

If you've been using Linux for a while, the chances of using this method to print environment variables are quite high (even if you were unaware that it's not an environment variable).

Now, let me show you how you're supposed to print the value of USERNAME with echo:

echo $USERNAME
Using echo command to print environment variable

But what if you want to print multiple environment variables using echo? just follow the given syntax:

echo -e "$<variable 1> \n<variable 2> \n<variable 3>"

So how about printing values of HOME, USERNAME, and HOSTNAME all at once using echo?

echo -e "$USERNAME \n$HOME \n$HOSTNAME"
Using echo to print values of HOME, USERNAME, and HOSTNAME

Using env Command

The env command is generally used by shell scripts to launch the correct interpreter but you can also use the env command to list available environment variables.

The env command if applied without any arguments and options, will print all the available environment variables.

env
Using the env command to print each environment variable available

But what if you want to get the value of a specific environment variable? you can easily filter results with the grep command.

For demonstration, I'll be printing out the value of HOME with the echo command:

env | grep HOME
Using env command to print HOME environment variable

Using declare Command

The declare command is used to declare and print the value of variables in the shell.

Like any other example shown above, you can use declare without any argument and will bring the list of available variables.

declare
Using declare command without any arguments to print available environment variables

Previously, I showed you how you can use the grep command to filter results but what if you want to filter more than one result?

First, let's have a look at the syntax for filtering multiple environment variables:

declare | grep '<variable 1>\|<variable 2>\|<variable 3>'

For example, let's print the value of HOSTNAME and USERNAME:

declare | grep 'HOSTNAME\|USERNAME'
Using grep with declare to print multiple environment variables

Using set Command

Generally, the set command is used to set and unset flags in the shell so that you determine the exact behavior for the upcoming process.

But you can also use the set command to print environment variables for the current shell.

To print each environment variable, use the given command:

set
Using set command to print available environment variables

You can use the same approach used in the above method to filter results from the set command.

But still, let's look at a simple example using the grep command to print multiple environment variables.

set | grep 'HISTFILESIZE\|HISTFILE\|GNOME_SHELL_SESSION_MODE'
Using the set command with grep to filter and print multiple environment variables

Wrapping up

Though there are various methods for printing environment variables, I'd highly suggest you use the first method the syntax is the least complicated in terms of filtering specific variables.

And if you are still confused, the comments section is open to you.