DEV Community

Cover image for Understanding Environment Variables
Madhav Ganesan
Madhav Ganesan

Posted on • Edited on

1 1 1 1 1

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 🤗

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay