List all environment variables from the command line
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To list all environment variables from the command line, use printenv on Linux/macOS or set on Windows Command Prompt. These commands dump every environment variable and its value in the current shell session. The exact command depends on your operating system and shell.
Linux and macOS
printenv
printenv lists all exported environment variables in NAME=value format:
To check a specific variable:
printenv only shows exported variables. Shell-local variables that were never exported do not appear.
env
The env command produces similar output to printenv:
The primary difference is that env can also run a command with a modified environment, which makes it useful in scripts:
Sorted Output
The output of printenv is not sorted by default. Pipe it through sort for readability:
Filtering Variables
Use grep to find variables matching a pattern:
The export and declare Commands in Bash
In Bash specifically, you can also list exported variables with:
The declare -p command shows both exported and local shell variables, along with their types (string, array, integer).
Windows
Command Prompt (cmd)
This displays all environment variables in the current session. To filter:
The set command in cmd performs prefix matching, so set JAVA shows all variables whose names start with JAVA.
PowerShell
PowerShell treats environment variables as items in the Env: drive, which means you can use all the standard filtering and formatting cmdlets.
PowerShell: Table vs. List Format
Checking a Single Variable
Each shell has its own syntax for reading a single variable:
| Shell | Command | Example |
| Bash / Zsh | echo $VARIABLE | echo $HOME |
| Bash / Zsh | printenv VARIABLE | printenv PATH |
| Fish | echo $VARIABLE | echo $HOME |
| cmd | echo %VARIABLE% | echo %PATH% |
| PowerShell | $env:VARIABLE | $env:PATH |
The echo approach shows the value as the current shell interprets it. printenv VARIABLE shows the exported value regardless of shell-local overrides.
Setting and Unsetting Variables
Linux / macOS
Windows cmd
Windows PowerShell
Making Variables Persistent
Setting a variable with export only affects the current session. To make it persist:
Linux / macOS
Add the export line to your shell's configuration file:
Reload the shell to apply:
Windows
Key Environment Variables Reference
| Variable | OS | Purpose | Typical value |
PATH | All | Directories searched for executables | /usr/bin:/usr/local/bin |
HOME | Linux/macOS | Current user's home directory | /home/username |
USERPROFILE | Windows | Current user's home directory | C:\Users\username |
USER | Linux/macOS | Current username | username |
USERNAME | Windows | Current username | username |
SHELL | Linux/macOS | Default shell path | /bin/zsh |
TERM | Linux/macOS | Terminal type | xterm-256color |
LANG | Linux/macOS | System locale | en_US.UTF-8 |
EDITOR | Linux/macOS | Default text editor | vim or nano |
JAVA_HOME | All | JDK installation path | /usr/lib/jvm/java-17 |
TMPDIR / TEMP | Linux/macOS / Windows | Temporary file directory | /tmp |
Using Environment Variables in Scripts
Dotenv Files for Local Development
The .env file pattern loads environment variables from a file during development without polluting the system environment:
Libraries like dotenv (Node.js), python-dotenv (Python), and godotenv (Go) load these files automatically. Never commit .env files to version control. Add .env to your .gitignore.
Common Pitfalls
Confusing printenv with set in Bash is a frequent issue. printenv shows only exported variables, while set (in Bash) shows all variables including local ones, shell functions, and other internal state. For a clean list of environment variables, printenv is the right choice.
Forgetting that export only affects the current session and its child processes means your variable disappears when you open a new terminal. Add persistent variables to the shell configuration file.
Setting sensitive values (API keys, passwords) as environment variables and then running printenv in a shared terminal or logging environment exposes those secrets. Be mindful of where environment variable output is captured.
On Windows, set in Command Prompt and set in PowerShell are different commands. PowerShell's set is an alias for Set-Variable, which manages PowerShell variables, not environment variables. Use Get-ChildItem Env: in PowerShell.
Not quoting variable values during assignment can cause word splitting in Bash. Always use export VAR="value with spaces" with quotes when the value contains spaces or special characters.
Summary
- Use
printenv(Linux/macOS) orset(Windows cmd) orGet-ChildItem Env:(PowerShell) to list all environment variables. - Pipe through
sortorgrepto filter and organize the output. - Use
export(Linux/macOS) or$env:(PowerShell) to set variables for the current session. - Add variables to shell configuration files (
~/.zshrc,~/.bashrc) for persistence across sessions. - Use
.envfiles with dotenv libraries for local development configuration. - Keep sensitive values out of plain
printenvoutput in shared or logged environments.

