What environment variables are and why you need them
An environment variable is a named value that your operating system stores and makes available to programs running on your computer. When a program needs information — like where to find Java, what user is logged in, or where temporary files should go — it asks the operating system for that variable by name instead of looking for it manually each time.
You add environment variables when you install software that needs a specific location to work, when you're writing code that needs to reference a path, or when you want to change how a program behaves without editing its files. For example, if you install the Java Development Kit, you'll need to set a JAVA_HOME variable so other tools know where to find it.
The steps differ depending on whether you use Windows, macOS, or Linux, and whether you want the variable available to just your user account or to everyone on the computer.
Key Takeaways
- Environment variables are named values your operating system stores and passes to programs — they're how software finds tools and configuration it needs.
- On Windows, you add them through System Properties or by editing your user profile file; on Mac and Linux, you edit a shell configuration file like .bashrc or .zshrc.
- User variables apply only to your account; system variables apply to all users and usually require administrator access.
- After adding a variable, you must close and reopen your terminal or command prompt for the change to take effect.
- You can check whether a variable was set correctly by typing echo $VARIABLE_NAME (Mac/Linux) or echo %VARIABLE_NAME% (Windows) in your terminal.
Adding environment variables on Windows
On Windows, the graphical method is the most straightforward. Open the Start menu, search for "Environment Variables," and click "Edit the system environment variables." In the System Properties window that opens, click the "Environment Variables" button near the bottom right.
You'll see two sections: "User variables for [your username]" and "System variables." Click "New" under whichever section you need. Type the variable name (for example, JAVA_HOME) in the first field and the value (for example, C:\Program Files\Java\jdk-17) in the second. Click OK, then OK again to close the windows.
If you prefer the command line, open Command Prompt as administrator and use the setx command. For a user variable, type setx VARIABLE_NAME "C:\path\to\value". For a system variable, type setx VARIABLE_NAME "C:\path\to\value" /M. The /M flag sets it system-wide; without it, the variable applies only to your user account.
After setting the variable, close and reopen Command Prompt or PowerShell completely. The new terminal window will have access to the variable.
Adding environment variables on macOS
On macOS, you edit a shell configuration file. Which file depends on which shell you're using — if you're unsure, open Terminal and type echo $SHELL. If the output ends in zsh, you're using the newer default shell; if it ends in bash, you're using the older one.
For zsh (the default on macOS 10.15 and newer), open Terminal and type nano ~/.zshrc. For bash, type nano ~/.bash_profile. The nano editor will open the file. Scroll to the end and add a new line: export VARIABLE_NAME="/path/to/value". For example, export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home".
Press Control+X, then Y, then Enter to save and exit. Close Terminal completely and reopen it. Type echo $VARIABLE_NAME to verify the variable was set — it should print the path you entered.
Adding environment variables on Linux
On Linux, the process is nearly identical to macOS. Open a terminal and determine your shell with echo $SHELL. Most modern Linux distributions use bash by default.
For bash, type nano ~/.bashrc. For zsh, type nano ~/.zshrc. For other shells like fish or ksh, the file names differ — check your shell's documentation. Scroll to the end of the file and add export VARIABLE_NAME="/path/to/value".
Save with Control+X, then Y, then Enter. Close the terminal and open a new one, or type source ~/.bashrc (or the appropriate file) to reload the configuration immediately. Verify with echo $VARIABLE_NAME.
The difference between user and system variables
A user variable applies only to your account and is stored in your user profile. A system variable applies to all users on the computer and is stored in a system-wide location. On Windows, you choose which section to add to in the Environment Variables dialog. On Mac and Linux, editing your personal shell configuration file (.bashrc, .zshrc) creates a user variable; editing system-wide files like /etc/environment or /etc/profile requires administrator access and affects all users.
For most purposes — installing development tools, setting up coding environments, or configuring personal projects — a user variable is sufficient and safer, because it won't affect other users or system processes. Use system variables only when you need the variable available to services that run as different users or when you're setting up a shared development machine.
Checking whether your variable was set correctly
After closing and reopening your terminal, verify the variable exists by printing its value. On Windows, open Command Prompt or PowerShell and type echo %VARIABLE_NAME% (note the percent signs). On Mac or Linux, type echo $VARIABLE_NAME (note the dollar sign).
If the variable was set correctly, the terminal will print the path or value you entered. If it prints the variable name itself (like "JAVA_HOME" instead of the path), the variable was not found — close the terminal completely, reopen it, and try again. If it still doesn't work, double-check the spelling of the variable name and the path you entered.
Some programs also have their own commands to check configuration. For example, if you set JAVA_HOME, you can type java -version to see whether Java can find its installation. Check the documentation for the specific tool you're configuring.
Common mistakes and how to fix them
The most common mistake is not closing and reopening the terminal after setting the variable. Your current terminal window was started before the variable existed, so it won't see it — only new windows will. Close the terminal completely (not just the tab) and open a fresh one.
The second mistake is using the wrong path. On Windows, paths use backslashes (C:\Program Files\Java) and must be enclosed in quotes if they contain spaces. On Mac and Linux, paths use forward slashes (/Library/Java) and spaces must be escaped with a backslash or the whole path enclosed in quotes. If you're unsure of the correct path, navigate to the folder in your file explorer or file manager, and copy the path from the address bar.
The third mistake is misspelling the variable name. Variable names are case-sensitive on Mac and Linux but not on Windows. If a tool's documentation says to set JAVA_HOME, don't type java_home or JavaHome — use exactly what the documentation specifies.
Frequently Asked Questions
Can I set an environment variable temporarily, just for one terminal session?
Yes. On Windows, type set VARIABLE_NAME=value in Command Prompt (not setx). On Mac or Linux, type export VARIABLE_NAME=value in your terminal. The variable will exist only in that terminal window and will disappear when you close it. This is useful for testing before you make a permanent change.
What if a program says it can't find a tool even though I set the environment variable?
First, verify the variable is set by printing it (echo $VARIABLE_NAME or echo %VARIABLE_NAME%). Second, check that the path you entered actually exists — navigate to it in your file manager to confirm. Third, make sure you closed and reopened the terminal after setting the variable. If the program is a service or daemon running in the background, you may need to restart it or restart your computer for it to see the new variable.
Do I need to restart my computer after adding an environment variable?
No, but you do need to close and reopen your terminal or command prompt. Some background services or IDEs may require a restart to see the new variable, but most command-line tools will work immediately in a new terminal window.
How do I remove or change an environment variable?
On Windows, open Environment Variables, select the variable you want to change, click Edit, and modify the value or click Delete to remove it. On Mac or Linux, open the configuration file (nano ~/.bashrc or nano ~/.zshrc), find the line with your variable, delete it or change the value, save, and close the terminal.
Can multiple programs share the same environment variable?
Yes. Many tools look for the same variables — for example, PATH is used by nearly every program to find executables. You can have one variable that multiple programs reference, or each program can have its own variable. Check each program's documentation to see what variables it expects.