Environment Variables
Command Line
Programming
Operating Systems
Software Development

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.

bash
1# Linux / macOS
2printenv
3
4# Windows Command Prompt
5set
6
7# Windows PowerShell
8Get-ChildItem Env:

Linux and macOS

printenv

printenv lists all exported environment variables in NAME=value format:

bash
printenv

To check a specific variable:

bash
printenv HOME
printenv PATH

printenv only shows exported variables. Shell-local variables that were never exported do not appear.

env

The env command produces similar output to printenv:

bash
env

The primary difference is that env can also run a command with a modified environment, which makes it useful in scripts:

bash
1# Run a command with an extra variable
2env DATABASE_URL="postgres://localhost/mydb" python app.py
3
4# Run a command with a clean environment
5env -i PATH=/usr/bin bash

Sorted Output

The output of printenv is not sorted by default. Pipe it through sort for readability:

bash
printenv | sort

Filtering Variables

Use grep to find variables matching a pattern:

bash
1# Find all AWS-related variables
2printenv | grep -i AWS
3
4# Find all variables containing "proxy"
5printenv | grep -i proxy
6
7# Find PATH-related variables
8printenv | grep PATH

The export and declare Commands in Bash

In Bash specifically, you can also list exported variables with:

bash
1# Shows exported variables with their declarations
2export -p
3
4# Shows all variables (including non-exported ones) in bash
5declare -p

The declare -p command shows both exported and local shell variables, along with their types (string, array, integer).

Windows

Command Prompt (cmd)

cmd
set

This displays all environment variables in the current session. To filter:

cmd
set PATH
set JAVA

The set command in cmd performs prefix matching, so set JAVA shows all variables whose names start with JAVA.

PowerShell

powershell
1# List all environment variables
2Get-ChildItem Env:
3
4# Sorted by name
5Get-ChildItem Env: | Sort-Object Name
6
7# Get a specific variable
8$env:PATH
9$env:HOME
10
11# Filter by name pattern
12Get-ChildItem Env: | Where-Object { $_.Name -match "JAVA" }

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

powershell
1# Default table format
2Get-ChildItem Env:
3
4# Detailed list format
5Get-ChildItem Env: | Format-List
6
7# Export to CSV
8Get-ChildItem Env: | Export-Csv -Path env_vars.csv -NoTypeInformation

Checking a Single Variable

Each shell has its own syntax for reading a single variable:

ShellCommandExample
Bash / Zshecho $VARIABLEecho $HOME
Bash / Zshprintenv VARIABLEprintenv PATH
Fishecho $VARIABLEecho $HOME
cmdecho %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

bash
1# Set a variable for the current session
2export MY_VAR="hello"
3
4# Set a variable for a single command
5MY_VAR="hello" ./script.sh
6
7# Unset a variable
8unset MY_VAR

Windows cmd

cmd
1:: Set a variable
2set MY_VAR=hello
3
4:: Remove a variable
5set MY_VAR=

Windows PowerShell

powershell
1# Set a variable
2$env:MY_VAR = "hello"
3
4# Remove a variable
5Remove-Item Env:MY_VAR

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:

bash
1# For Zsh (default on macOS)
2echo 'export MY_VAR="hello"' >> ~/.zshrc
3
4# For Bash
5echo 'export MY_VAR="hello"' >> ~/.bashrc
6
7# System-wide (all users)
8# Add to /etc/environment or create a file in /etc/profile.d/

Reload the shell to apply:

bash
source ~/.zshrc

Windows

powershell
1# Set a persistent user-level variable
2[System.Environment]::SetEnvironmentVariable("MY_VAR", "hello", "User")
3
4# Set a persistent machine-level variable (requires admin)
5[System.Environment]::SetEnvironmentVariable("MY_VAR", "hello", "Machine")

Key Environment Variables Reference

VariableOSPurposeTypical value
PATHAllDirectories searched for executables/usr/bin:/usr/local/bin
HOMELinux/macOSCurrent user's home directory/home/username
USERPROFILEWindowsCurrent user's home directoryC:\Users\username
USERLinux/macOSCurrent usernameusername
USERNAMEWindowsCurrent usernameusername
SHELLLinux/macOSDefault shell path/bin/zsh
TERMLinux/macOSTerminal typexterm-256color
LANGLinux/macOSSystem localeen_US.UTF-8
EDITORLinux/macOSDefault text editorvim or nano
JAVA_HOMEAllJDK installation path/usr/lib/jvm/java-17
TMPDIR / TEMPLinux/macOS / WindowsTemporary file directory/tmp

Using Environment Variables in Scripts

bash
1#!/bin/bash
2# Check if a required variable is set
3if [ -z "$DATABASE_URL" ]; then
4    echo "ERROR: DATABASE_URL is not set"
5    exit 1
6fi
7
8echo "Connecting to: $DATABASE_URL"
python
1import os
2import sys
3
4db_url = os.getenv("DATABASE_URL")
5if db_url is None:
6    print("ERROR: DATABASE_URL is not set", file=sys.stderr)
7    sys.exit(1)
javascript
1const dbUrl = process.env.DATABASE_URL;
2if (!dbUrl) {
3    console.error("ERROR: DATABASE_URL is not set");
4    process.exit(1);
5}

Dotenv Files for Local Development

The .env file pattern loads environment variables from a file during development without polluting the system environment:

bash
1# .env file
2DATABASE_URL=postgres://localhost/mydb
3REDIS_URL=redis://localhost:6379
4DEBUG=true

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) or set (Windows cmd) or Get-ChildItem Env: (PowerShell) to list all environment variables.
  • Pipe through sort or grep to 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 .env files with dotenv libraries for local development configuration.
  • Keep sensitive values out of plain printenv output in shared or logged environments.

Course illustration
Course illustration

All Rights Reserved.