DEV Community

Cover image for Understanding Environment Variables
Madhav Ganesan
Madhav Ganesan

Posted on • Updated on

Understanding Environment Variables

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

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

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

2) Click the "Environment Variables" button.

Figure 4

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

Figure 5

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

Figure 6

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

Environment variables in CMD:

1)Viewing Environment Variables:

set
Enter fullscreen mode Exit fullscreen mode

2)View a Specific Environment Variable:

echo %VARIABLE_NAME%
Enter fullscreen mode Exit fullscreen mode

3)Setting a Variable (Temporarily):

set MY_VARIABLE=VALUE
Enter fullscreen mode Exit fullscreen mode

4)Setting a Variable (Permanently):

setx MY_VARIABLE VALUE
Enter fullscreen mode Exit fullscreen mode

Feel free to comment below with any questions or tips about this particular topic. Thank you 🤗

Top comments (0)