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.
Table of Contents
Variable1 = C:\xyz
Variable2 = 6
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
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 :
and press Enter.
Some of the most common environment variables:
|%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:
If you want to save this variable for a future look then you can save it in a text file with the following command.
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:
If you want help and wants to know more about set cmdlet then type the below command in command prompt for help:
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.
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.
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.
Using reg query command in Command Prompt
If you just want System variables then type below in the command prompt
If you just want User variables then type below in the command prompt.
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.
In Linux, you can simply use the below command for displaying the list of environment variables.
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.
In this approach, we may get a list of all the environment variables in major operating systems such as Windows, Linux, and Mac.