How to echo environment variable on Linux

Environment variables contain data about the current system configuration. These variables are mostly referenced by scripts and system programs that need some information on the current configuration in order to adapt to various scenarios. For example, a script might check an environment variable to see what language is set on the computer, and then output prompts in the target language. One of the most commonly accessed environment variables is the PATH environment variable.

Environment variables can be seen by any user on a Linux system by using the echo Linux command, among other methods. In this tutorial, you will learn how to echo an environment variable on a Linux system. This can be used to attain some configuration information or can be worked into a Bash script to make it respond differently depending on the results.

In this tutorial you will learn:

  • How to echo environment variable on Linux
How to echo environment variable on Linux
How to echo environment variable on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
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

How to echo environment variable




We can echo an environment variable in Linux by using the echo command and the name of the variable we want to check. Here are some examples:

$ echo $SHELL
/bin/bash

$ echo $HOME
/home/linuxconfig

$ echo $USER
linuxconfig

Let’s see how to make this a little more useful:

$ echo "The current user is $USER, with the $SHELL shell, and has a home directory located at: $HOME"
The current user is linuxconfig, with the /bin/bash shell, and has a home directory located at: /home/linuxconfig

With the example above, it is easy to see how this functionality could be handy when writing a Bash script. It would allow a script to address the user by name, or create and manipulate directories or other system content by knowing where the home directory and other important things are located.

Echo all variables

Since it would not be reasonable to expect someone to know all the different pre-programmed variable names, you can get a full list of them by using the printenv command:

$ printenv




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

DID YOU KNOW?
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.

Closing Thoughts

In this tutorial, we saw how to echo 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.