How to list all environment variables ?


👤 Diwas Poudel    🕒 30 May 2022    📁 TECH

In computer programming, variables are the storage locations for values, and the values themselves can be anything. It could be a number, a filename, or some other piece of data. In a similar fashion, there are things called Environment Variables in your Operating System.

Environment Variables are global variables that store values linked to the current working environment and can be utilized by the operating system and other applications to retrieve necessary operating system information. Environment Variables are also known as env variables. Environment Variables are available in Windows, Mac, and Linux.

They are created as pairs consisting of a number and a value, and both the operating system and the application are able to access those values. The General Format of environment value looks like this:

Variable1 = C:\xyz
Variable2 = 6
variable2 ='val1:val2'
variable4 = val1;val2
...

These types of environment variables can be viewed, created, edited, and deleted. Environment Variable in general helps applications to know which directory to install installable files, and where to find user and system settings.

Some of the environment variables are PATH, USER, HOME, UID, SHELL, TERM, EDITOR, etc.PATHTEXT, OS, TEMP,SYstemRoot.PATH is a popular Environment variable that is available in Windows, Mac, and Linux.

If you want to know what environment variables are used in various OS then I will guide you on this.

List of Environment variables in Windows Environment 

There are actually two types of environment variables in Windows. They are :

1. Local/User Environment Variable: User Variables store information that is unique to the logged-in user and are only relevant during the current session. They have located under the "HKEY_CURRENT_USER/Environment" path in the Registry

2. System Environment Variable: Because they are set uniformly across all of your computer's users, system variables don't contain any user-specific data. All user accounts are affected by these global variables.They are located under "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/ControlSession /ManagerEnvironment" path in Registry

Learn More at System Variables vs User Variables 

There is various way to list environment variables in windows. Let's discuss some of them.

List environment variables using Command Line

In the command prompt type :

SET

and press Enter.

set-environment-variable
fig. List all windows environment variable

Some of the most common environment variables:

Environment Variables Description
%APPDATA% AppData contains data and settings of various installed programs on your PC.
%COMPUTERNAME% The unique identifier is given to your computer
%HOMEPATH% HomePath describes the path for the user's personal files. It contains user files like downloads, Desktop, Documents, Videos, Picture, Contacts, and many more.
%PATH% The PATH variable provides the directories on the machine where executable programs can be found.
%CD% Returns the current directory string.
%DATE% The most recent date, formatted according to the Date command.
%LOGONSEVER% This function brings back the name of the domain controller that was responsible for validating the current logon session.
%NUMBER_OF_PROCESSORS% This number indicates how many processors are currently installed on the computer.
%OS% This function returns the name of the operating system.
%PROMPT% The directory in which the operating system is stored is returned by this function.
%WINDIR% The directory in which the operating system is stored is returned by this function.
%USERNAME% This function gets you the name of the user who is logged in at the moment.
%RANDOM% It returns a random decimal number between 0 and 32767. 

If you have a large number of system and user variables in environment variables then SET displays all at a time. But if you want one page and then one line at a time then try this:

SET | more

set-pipe-more

If you want to save this variable for a future look then you can save it in a text file with the following command.

SET > any_name.txt
fig. output.txt contains an environment variable path

Here, I have used output.txt as the file name, and this file gets saved under C:\User\Dell path and contain output.txt look like this:

output-environmentvariable

If you want help and wants to know more about set cmdlet then type the below command in command prompt for help:

set /?
Note: All commands used here are case insensitive. So, Get-ChildItem Env: is equivalent to get-childitem env:

List environment variables using Powershell

Get-ChildItem (GCI) is a useful cmdlet that is used to get items and, if the item is a container, it will get child items that are available inside the container.

You can simply type below one line to get all environment variables.

Get-ChildItem Env:

or Simply

gci env:

powershell-environmentvariable

As you can see, some lines of the output are truncated. If you want to get the full output without any truncation, use the cmdlet below.

gci Env: | Format-Table -Wrap -AutoSize

or

Get-ChildItem Env: | Format-Table -Wrap -AutoSize

The gci env: cmdlet retrieves all environment variables, which are then passed through the pipeline(|) to Format-Table and displayed in a table. The AutoSize option adjusts column widths to avoid truncation.-Wrap Displays text that exceeds the column width in the new line without truncation.

Below, we can see that no lines in the output are truncated, as they were previously.

gci-cmdlet-without-truncationUsing reg query command in Command Prompt

If you just want System variables then type below in the command prompt

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

If you just want User variables then type below in the command prompt.

reg query HKEY_CURRENT_USER\Environment

List of Environment variables in Linux Environment  

You can use the below cmdlet in Linux for getting environment variables. I am testing in ubuntu and it must work in other distributions of Linux.

printenv

ubuntu-machine

In Linux, you can simply use the below command for displaying the list of environment variables.

env

It is not possible to view all bash variables when using the env command since it will only show a list of environment variables that have been exported.

List of Environment variables in macOS Environment  

For macOS, we can use printenv for displaying all environment variables.

printenv

Conclusion:

In this approach, we may get a list of all the environment variables in major operating systems such as Windows, Linux, and Mac.