Understanding Environment Variables
Tags: operatingsystems, beginners, programming, tutorial
Published: 2024-06-20 15:15:30

Environment variables are required for configuring the operating system and applications without hardcoding values. They can be set at two levels: **user-level** and **system-level**.



## User Environment Variables

**Scope**: Only affect the user account under which they are set.

**Use Case**: Ideal for settings that are specific to a single user and do not need to affect other users on the system.


![Figure 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7f137glkq1ssyl647qr.jpg)



## System Environment Variables

**Scope:** Affect all user accounts on the system.

**Use Case:** Ideal for settings that should be available to all users, such as paths to critical software or libraries required by multiple applications.

![Figure 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1q3qgget8ffly3atkzjt.jpg)



## The PATH Variable
The PATH variable is one of the most important environment variables in an operating system. It specifies a list of directories that the system searches to find executable files. 

When you run a command in the terminal or command prompt, the system looks through the directories listed in the PATH variable to find the executable file for that command.
 

## How the PATH Variable Works?

**Execution of Commands:**
When you type a command (e.g., javac, node) and press Enter, the system looks for the corresponding executable file in the directories listed in the PATH variable.

Example: **Running Node from the Command Line**
Let's say you have installed Node on your system and the executable is located in the directory C:\NodeMain. To run Node from any directory in the command prompt, you need to add C:\NodeMain to your PATH variable.

**Set the PATH Variable:**

1) Open the Start Search, type in "env", and select "Edit the system environment variables".

![Figure 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bdlqksvlq966xpfdypv.jpg)

2) Click the "Environment Variables" button.

![Figure 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pynxitnd3il51xggwpgb.jpg)

3) In the "System variables" section, find the PATH variable and click "Edit".


![Figure 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yfa6xxt3q9q088ba6t4e.jpg)

4) Add C:\NodeMain to the list of paths.

![Figure 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x96yzehxg6lsnjptx77j.jpg)

**Run Node:**

Open a new terminal or command prompt.
Type node and press Enter. The system will look through the directories listed in the PATH variable and find the Node executable in C:\NodeMain, launching the Node interpreter.

![Figure 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvu3lhqywmipv0mnf71e.jpg)
 

## Environment variables in CMD:

1)**Viewing Environment Variables**:
```
set
```

2)**View a Specific Environment Variable:**
```
echo %VARIABLE_NAME%
```

3)**Setting a Variable (Temporarily)**:
```
set MY_VARIABLE=VALUE
```

4)Setting a Variable (Permanently):
```
setx MY_VARIABLE VALUE
```
-----------------------------------------------------------------
Feel free to comment below with any questions or tips about this particular topic. Thank you 🤗