How to Add an Environment Variable in Windows, macOS, and Linux
Environment variables are one of those behind-the-scenes tools that quietly power a huge amount of how your computer and software behave. Whether you're setting up a development environment, configuring a command-line tool, or pointing software to the right directory, knowing how to add an environment variable is a practical skill that pays off fast.
What Is an Environment Variable?
An environment variable is a named value stored at the operating system level that programs can read at runtime. Instead of hardcoding a file path, API key, or configuration setting directly into an application, developers (and the OS itself) use environment variables as flexible placeholders.
For example:
PATHtells your system where to look for executable programsHOMEstores the path to the current user's home directoryNODE_ENVtells a Node.js application whether it's running in development or production mode
These variables exist in a key=value format. The key is the variable name (usually uppercase by convention), and the value is the string it holds.
There are two main scopes to understand:
| Scope | Who Sees It | Persists After Reboot? |
|---|---|---|
| System-wide | All users and processes | Yes |
| User-level | Only the current user | Yes |
| Session-level | Only the current terminal session | No |
Which scope you need depends entirely on what the variable is for.
How to Add an Environment Variable on Windows 🖥️
Windows gives you two routes: a graphical interface and the command line.
Using the GUI (System Properties)
- Press Windows + S and search for Edit the system environment variables
- Click Environment Variables at the bottom of the System Properties window
- Under User variables (for your account only) or System variables (for all users), click New
- Enter the Variable name and Variable value
- Click OK through all open windows
Changes take effect for any new processes you launch. Already-open terminals won't see them until restarted.
Using the Command Line
To set a session-only variable in Command Prompt:
set MY_VARIABLE=myvalue To set a persistent user-level variable via PowerShell:
[System.Environment]::SetEnvironmentVariable("MY_VARIABLE", "myvalue", "User") To set a system-wide persistent variable (requires admin rights):
[System.Environment]::SetEnvironmentVariable("MY_VARIABLE", "myvalue", "Machine") The PATH variable is special — when adding to it, you append rather than replace, using a semicolon as a separator on Windows.
How to Add an Environment Variable on macOS
macOS handles environment variables primarily through shell configuration files. Which file you edit depends on your shell.
Identify Your Shell First
Run echo $SHELL in Terminal. You'll see either /bin/zsh (default since macOS Catalina) or /bin/bash (older systems).
Editing the Shell Profile
For Zsh (most common):
nano ~/.zshrc For Bash:
nano ~/.bash_profile Add your variable at the bottom of the file:
export MY_VARIABLE="myvalue" Save the file, then apply the changes to your current session:
source ~/.zshrc These variables persist across reboots and are user-specific. For system-wide variables visible to all users and GUI applications, macOS requires more advanced configuration using launchd — relevant mainly in server or multi-user scenarios.
How to Add an Environment Variable on Linux 🐧
Linux follows a similar pattern to macOS, since both are Unix-based systems.
For the Current Session Only
export MY_VARIABLE="myvalue" This works immediately but disappears when the terminal closes.
For a Specific User (Persistent)
Edit ~/.bashrc (for Bash) or ~/.zshrc (for Zsh), adding:
export MY_VARIABLE="myvalue" Then run source ~/.bashrc to apply immediately.
For All Users System-Wide
Edit /etc/environment (on most Debian/Ubuntu-based systems):
MY_VARIABLE="myvalue" This file doesn't use export — just bare KEY=VALUE pairs. Changes apply after a reboot or re-login.
Alternatively, you can drop a file into /etc/profile.d/ — a common approach on Red Hat-based distributions.
The PATH Variable: A Special Case
PATH is the most commonly modified environment variable. It tells your OS which directories to search when you type a command. Adding a directory to PATH makes executables in that directory available globally.
On Unix systems, directories in PATH are separated by colons:
export PATH="$PATH:/usr/local/myapp/bin" On Windows, semicolons separate entries. Always append to the existing value rather than overwriting it — replacing PATH entirely is a common mistake that breaks basic system commands.
Factors That Affect Your Approach
Several things shape which method makes sense:
- Operating system and version — macOS Catalina and later default to Zsh; older Macs use Bash. Windows 10/11 PowerShell commands differ from older
setxapproaches - Scope needed — a personal development tool only needs user-level scope; a background service running under a system account needs system-level
- Whether it's sensitive data — storing API keys or credentials as environment variables is common practice, but where those files live and who can read them matters for security
- Shell being used — Bash, Zsh, Fish, and PowerShell each have their own syntax and configuration file conventions
- Application type — some frameworks (Docker, Node.js, Python's
dotenv) have their own mechanisms for loading environment variables that sit alongside OS-level ones
A developer running multiple projects locally has very different needs than someone adding a single tool to their PATH for personal use — and the right approach in each case reflects that difference.