How to print environment variables on Linux

The environment variables on a Linux system contain changing values that are referenced mainly by scripts and system programs. Environment variables differ from shell variables, because they can be accessed by any user or process across the entire system. In this tutorial, you will learn how to print environment variables on Linux.

In this tutorial you will learn:

  • What is an environment variable?
  • How to print an individual environment variable
  • How to print all environment variables on a Linux system
How to print environment variables on Linux
How to print environment variables on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

What is an environment variable?




Let’s look at a very simple example to see how environment variables work and why they exist. There are many system programs and user made scripts that need to access a current user’s home directory. So, how would we tell a Bash script to access a home directory, since this directory is going to keep changing, depending on which user is executing the script?

Tha answer is that we would use the HOME environment variable. This is a variable which will continue to change its value to the current user’s home directory. Therefore, a script that contains the following line can be used by any user on the system and it will generate the same result.

$ mkdir $HOME/.logs

Another common environment variable is SHELL, which will always contain the path to the user’s current shell.

$ echo $SHELL > $HOME/current-shell.log

$ cat $HOME/current-shell.log
/bin/bash

There are a lot of other environment variables in addition to these two. Keep reading to learn about more.

How to print environment variables

The printenv command can be used to list all environment variables on a Linux system. Keep in mind that some of these values will change, depending on which user is logged in.

$ printenv
Printing all the environment variables used by this Linux system
Printing all the environment variables used by this Linux system

To list a specific variable, just pass the name of it to the command.

$ printenv SHELL
/bin/bash

You can also check multiple variables simultaneously.

$ printenv HOME SHELL
/home/linuxconfig
/bin/bash

To interact with the environment variables in your terminal or when writing a Bash script, you will need to precede them with a dollar sign $.

$ echo "I am logged in as $USER with the $SHELL shell and my home directory is $HOME"
I am logged in as linuxconfig with the /bin/bash shell and my home directory is /home/linuxconfig

A popular environment variable to edit is the $PATH variable, which lets you specify the directories Bash should search for programs when you enter a command. We’ve written a separate guide on how to add a directory to $PATH.



$ printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

To see more information about setting environment variables on Linux, see our tutorials on How to set and list environment variables on Linux and export command in Linux with examples.

Closing Thoughts

In this tutorial, we saw how to print environment variables on a Linux system. Environment variables are a useful convention in Linux shells that help facilitate system processes and user scripts. Without environment variables, we would not be able to obtain the type of information which can constantly change depending on different scenarios, such as which user is logged in, which desktop GUI is used, which directory a user is in, etc.



Comments and Discussions
Linux Forum