Check Environment Variable: Commands for Windows, Linux and macOS

Published:

Ever spent an hour chasing a missing environment variable that broke your build or stopped a service?
Checking env vars is the first stop, fast to do, faster to fix.
This post shows one-line commands for Windows (CMD and PowerShell), Linux, and macOS so you can list values, check a single key, and verify PATH quickly.
You’ll also get quick existence checks, container tips for Docker and Kubernetes, and the key gotcha: Windows is case-insensitive, Linux/macOS are not.
Read the commands, run the quick checks, and stop guessing what’s in the environment.

Quick Ways to Check Environment Variables on Any System

OEKDLk5OSkWEvNe69scQ6A

Environment variables store configuration your OS and apps need to run. Want to debug a broken PATH, confirm a runtime setting, or make sure a build will use the right key? Checking these variables is the first stop.

Most systems give you a GUI and command-line options. Developers usually reach for the terminal because it’s faster and scriptable. Try these quick commands.

  1. Windows Command Prompt. Open CMD (Win+R then cmd) and run set to list everything. Check a single value with echo %PATH% or echo %USERNAME%.
  2. Windows PowerShell. Run Get-ChildItem Env: to list variables. Print one with $Env:PATH or (Get-ChildItem Env:PATH).Value.
  3. Linux and macOS. In Terminal use printenv or env to list all variables. Check one with echo $PATH or printenv PATH.
  4. Verify existence on Windows. Test-Path Env:MY_VAR returns True or False.
  5. Verify existence on Linux/macOS. printenv MYVAR prints the value if set, or use [ -z “$MYVAR” ] && echo “not set” || echo “set”.
  6. PATH specifics. echo %PATH% on Windows CMD, $Env:Path in PowerShell, echo $PATH on Linux/macOS. Windows uses semicolons as separators. Linux and macOS use colons.

One key difference to remember. Windows treats variable names as case-insensitive. Linux and macOS do not.

Methods to Check Environment Variables in Windows (CMD, GUI, PowerShell)

QVK-kZlzSnK-qb2F46niNg

Windows gives three common routes: the System Properties GUI, Command Prompt, and PowerShell. Each one fits different needs. Want a safe visual edit? Use the GUI. Need a quick check or a scriptable change? Use the command line.

The GUI is explicit. Right-click Start, pick System, click Advanced system settings, then Environment Variables. You’ll see User variables and System variables. Click Edit to change values. No memorization required. But editing system variables needs admin rights.

CMD is fast when you know names. Win+R, type cmd, then set to list session variables. Use echo %VARIABLENAME% to print one. Remember set MYVAR=TestValue only affects the current window.

PowerShell is script-friendly. Get-ChildItem Env: lists variables. $Env:MYVAR reads one. Assigning $Env:MYVAR = “Value” is session-only. To persist a user variable run [System.Environment]::SetEnvironmentVariable(“MY_VAR”,”TestValue”,”User”). Use “Machine” for system-wide changes and run as admin.

Method Example Command Notes
GUI Start → System → Advanced system settings → Environment Variables Visual; separates User and System scopes; viewing needs no admin; editing Machine scope requires admin
CMD set
echo %PATH%
Lists variables for the session; set assignments are temporary
PowerShell (session) Get-ChildItem Env:
$Env:PATH
Good for scripts; session changes are temporary
PowerShell (persistent) [System.Environment]::SetEnvironmentVariable(“MY_VAR”,”TestValue”,”User”) Survives reboot for the chosen scope; Machine requires admin; some apps need restart to pick up changes

Always back up PATH before editing it. One missing semicolon can break many commands. Prefer appending rather than overwriting.

Checking Environment Variables on Linux and macOS Terminals

JaalHlqST965ZMJPgMp2ZA

Linux and macOS use similar shells, so the commands overlap. printenv or env shows current environment variables one per line. Want to page the output? Pipe to less: printenv | less. To print a single variable, run printenv PATH or echo $PATH.

set shows more than environment variables. It lists shell variables and functions too. If you want to compare exported environment variables against shell-local ones, run set | sort and env | sort and compare the outputs. Careful with syntax when using process substitution on some shells.

Common commands and uses:

  • printenv — list or print a named variable.
  • env — list or run a command with temporary variables, e.g., env VAR=value command.
  • echo $VARIABLE_NAME — quick read of one value.
  • set — shows shell variables, environment variables, and functions.
  • grep filtering — narrow results, like printenv | grep PATH.

Login vs non-login shells matter for persistence. Login shells read /etc/profile then the first of ~/.bashprofile, ~/.bashlogin, or ~/.profile. Interactive non-login shells read /etc/bash.bashrc and ~/.bashrc. Most people put export MY_VAR=”value” in ~/.bashrc and run source ~/.bashrc after editing. System-wide variables often live in /etc/environment or /etc/profile.

After editing, open a new terminal tab and echo $MY_VAR to confirm persistence. If it’s empty, double-check which init file your shell uses and ensure you used export.

How to Check Environment Variables Inside Scripts (Bash, PowerShell, Node.js, Python)

94v5annCQk2UeDmL3Cc5Aw

Scripts inherit environment variables from their parent, but each language has its own access patterns. Validate critical variables early and fail fast if something is missing.

Bash

echo "PATH is: $PATH"
if [ -z "$MY_API_KEY" ]; then
  echo "MY_API_KEY is not set"
  exit 1
fi
# default fallback
API_URL=${API_URL:-https://example.com}

PowerShell

Write-Host "PATH is: $Env:Path"
if (-not (Test-Path Env:MY_API_KEY)) {
  Write-Error "MY_API_KEY is not set"
  exit 1
}
# default fallback
$value = if ($Env:MY_VAR) { $Env:MY_VAR } else { "default" }

Node.js

console.log('PATH is:', process.env.PATH);
if (!process.env.MY_API_KEY) {
  console.error('MY_API_KEY is not set');
  process.exit(1);
}
const url = process.env.API_URL || 'https://example.com';

Python

import os
print('PATH is:', os.environ['PATH'])
api_key = os.getenv('MY_API_KEY')
if not api_key:
    print('MY_API_KEY is not set')
    exit(1)

Best practices across languages. Check required variables at startup. Use inline defaults for optional values. Never hardcode secrets in the repo. Note platform differences: case sensitivity on Linux and macOS, case-insensitivity on Windows.

Checking Environment Variables in Docker and Kubernetes Workloads

vOF0Bs-VRMOg4gKtaoefVg

Containers get environment variables at startup. Change the config and restart the container or recreate the pod to apply new values.

For Docker, set variables with docker run –env, docker-compose environment or envfile, or Dockerfile ENV. To inspect a running container, docker exec printenv lists all variables. To check one value use docker exec sh -c ‘echo $MYVAR’. Or drop into a shell with docker exec -it sh.

Kubernetes injects variables via the env field, envFrom for ConfigMaps and Secrets, or by mounting them as files. Verify values with kubectl exec — printenv or kubectl exec — sh -c ‘echo $MY_VAR’. Updating a ConfigMap or Secret does not update running pods automatically. You need to recreate the pod or trigger a rollout.

Platform How to List All How to Check Single Notes
Docker CLI docker exec <container_id> printenv docker exec <container_id> sh -c ‘echo $MY_VAR’ Set with –env or ENV in Dockerfile; variables fixed at container start
docker-compose docker-compose exec <service> printenv docker-compose exec <service> sh -c ‘echo $MY_VAR’ Define in environment or env_file; restart service to apply changes
Kubernetes Pod kubectl exec <pod-name> — printenv kubectl exec <pod-name> — sh -c ‘echo $MY_VAR’ Set via env, envFrom, or mounts; recreate pod to pick up ConfigMap or Secret updates
Kubernetes ConfigMap kubectl get configmap <name> -o yaml kubectl exec <pod-name> — sh -c ‘echo $MY_VAR’ Use envFrom to inject keys; pod restart needed for updates unless using mounted files with a loader
Kubernetes Secret kubectl get secret <name> -o yaml kubectl exec <pod-name> — sh -c ‘echo $MY_VAR’ Values are base64-encoded in YAML; use RBAC to limit access

If a variable is missing in a container, first confirm it’s defined in the Dockerfile, compose file, or deployment YAML. Then check capitalization and typos. For Docker inspect what was set with docker inspect and look under Config.Env.

Safe Ways to Check and Edit Critical Variables Like PATH, JAVAHOME, and NODEENV

V4kc5oDQciwObULHSHWKg

PATH, JAVAHOME, and NODEENV break things fast when wrong. Verify them before builds and deployments.

PATH. Print it first. On Windows use echo %PATH% or $Env:Path in PowerShell. On Linux and macOS use echo $PATH. Back up the value, then append instead of overwriting. Verify commands like which node or java -version after editing.

JAVAHOME. Point it to your JDK root. Check with echo %JAVAHOME% or echo $JAVAHOME. Run java -version to confirm the JDK on PATH matches JAVAHOME.

NODEENV. Many frameworks switch behavior based on this value. Check it with echo %NODEENV% on Windows or echo $NODE_ENV on Linux and macOS. If it’s unset, Node defaults to development behavior.

Quick safety checklist:

  • Back up PATH before edits.
  • Confirm directories exist after setting values.
  • Test the change immediately with the relevant command.
  • Use export on Linux and macOS so variables reach child processes.
  • Document where each variable is defined.

Troubleshooting Environment Variable Issues Across Platforms

zyfIa1jlTbC40FSVp8NciA

Most problems fall into a few buckets. The variable isn’t set. The scope is wrong. A running process cached the old value. Or conflicting definitions override what you expect.

Quick checklist to debug:

  1. Wrong scope. Decide whether the variable should be user or system level and set it in the correct place.
  2. Missing export on Linux and macOS. If echo shows a value but printenv does not, add export.
  3. Login vs non-login shells. Confirm which init file your shell reads.
  4. PATH separator mistakes. Use semicolons on Windows and colons on Linux and macOS.
  5. Permission errors. System-wide changes often need admin or sudo.
  6. Cached values. Restart the app, reopen the terminal, or sign out and back in.
  7. Wrong shell for scripts. Check the shebang and the active shell.
  8. Secrets in plaintext. Don’t commit API keys. Use environment variables and proper file permissions or a secret manager.

After any change, verify with echo, printenv, or $Env:. Open a new shell to confirm persistence. Document where each variable lives so the next person, or future you, won’t spend hours hunting it down.

Final Words

We ran commands across Windows, Linux/macOS, Docker/Kubernetes, and inside scripts to list, print, and test variables, plus safe edits for PATH, JAVAHOME, and NODEENV.

Quick gotchas: back up PATH before editing; Windows is case-insensitive while Linux/macOS aren’t; container variables are fixed at startup and need a restart to change.

When you need to check environment variable, use the commands here, verify results, and keep a backup. Small checks now save big debugging later — you’ve got this.

FAQ

Q: How do you check an environment variable?

A: You check an environment variable by reading its value in your shell: on Linux/macOS use echo $VAR or printenv VAR; on Windows CMD use echo %VAR%; PowerShell uses $Env:VAR.

Q: How to verify environment variable?

A: You verify an environment variable by confirming its value matches expectations with the same commands: echo $VAR / printenv (Linux/macOS), echo %VAR% (CMD), or $Env:VAR (PowerShell), plus existence checks.

Q: How do I see environment variables in CMD?

A: In CMD you see environment variables by running set to list all, or echo %VAR% to show one; use set | findstr NAME to filter and find a specific variable quickly.

Q: How do I get a list of all environment variables?

A: To get a list of all environment variables use env or printenv on Linux/macOS, set in CMD, or Get-ChildItem Env: in PowerShell; pipe to less or grep to search.

curtisharmon
Curtis has spent over two decades guiding hunters and anglers through the backcountry of Montana and Wyoming. His expertise in elk hunting and fly fishing has made him a sought-after voice in the outdoor community. Curtis combines traditional woodsmanship with modern techniques to help readers succeed in the field.

Related articles

Recent articles