Print Environment Variable: Fast Commands for Every System

Published:

Think printing an environment variable is trivial, until your deploy fails at 2 AM?
This post gives fast, copy-paste commands to print env vars on Linux, macOS, Windows (CMD and PowerShell), and from code.
You’ll get quick filtering tips, safe output methods for scripts and containers, and the exact commands to save time when debugging.
The thesis: learn one command per system and you’ll cut lookup-and-fix time in half.
No setup: copy, paste, run.

Essential Commands to Print Environment Variables Across All Systems

m3tSjmi4SG-N0fu619VM3w

Environment variables store configuration data, system paths, API keys, and runtime settings your shell, application, or OS needs. Printing these variables is something you’ll do constantly when debugging broken paths, checking loaded secrets, or verifying config before deployment.

On Linux and macOS, echo $HOME prints the value of HOME, and printenv lists all environment variables. For a single variable on Linux or macOS, use printenv HOME or echo $HOME.

On Windows, use echo %HOME% in Command Prompt or $Env:HOME in PowerShell. To list all variables on Windows, run set in CMD or Get-ChildItem Env: in PowerShell.

Copy and paste commands for printing environment variables:

  • Linux/macOS single variable: echo $HOME
  • Linux/macOS all variables: printenv
  • Linux/macOS single variable (alternate): printenv HOME
  • Windows CMD single variable: echo %HOME%
  • Windows CMD all variables: set
  • Windows PowerShell single variable: $Env:HOME
  • Windows PowerShell all variables: Get-ChildItem Env:
  • Portable printf (Linux/macOS): printf '%s\n' "$HOME"

Methods to Print Environment Variables in Linux and macOS Terminals

3g2G2ZxbTR-Ym62Iw4CDeA

The way you print environment variables depends on your shell. Bash, zsh, and fish all support similar syntax, but some commands like set and declare behave differently across shells. Most examples below work in bash and zsh, the two most common shells. Fish uses its own syntax for variable expansion but still supports printenv and env.

The simplest command is printenv. Run printenv USERNAME to print a single variable, printenv HOME USERNAME to print two variables, or printenv with no arguments to list every environment variable. The output shows each variable on its own line in VAR=value format.

Use echo $VARIABLE for quick inline checks. Example: echo $USERNAME prints the value of USERNAME. To print multiple variables in one line, run echo $HOME $USERNAME $HOSTNAME. Always quote expansions when the value might contain spaces: echo "$HOME" preserves whitespace and special characters. For safer output that handles newlines and escape sequences, use printf '%s\n' "$HOME" instead of echo.

The env command lists all environment variables when run without arguments. Unlike set or declare, env only shows variables exported to child processes, not shell-local variables or functions. Pipe env to less for paging long output: env | less. You can also run env with a modified environment to test changes without persisting them.

The declare command prints all shell variables, including those not exported. Run declare alone to see the full list. The set command prints shell variables and function definitions, making the output longer and harder to scan. Both are useful when you need to inspect local shell state, not just environment variables. Use declare -p VARIABLE to print the declaration and type of a specific variable.

Filtering printed results using grep

When you have hundreds of variables, grep narrows results to what you need. Run env | grep HOME to show only lines containing HOME. For case insensitive search, add the i flag: env | grep -i path. To match multiple patterns, use grep with the E flag and a pipe separated regex: declare | grep -E 'HOSTNAME|USERNAME' returns lines matching either variable. Combine printenv with grep for fast filtering: printenv | grep PATH shows PATH and any variable name containing PATH.

Printing Environment Variables on Windows (CMD and PowerShell)

wP38xJw4SU6hYAMj3qH_rQ

Windows provides two main shells for printing environment variables: Command Prompt (CMD) and PowerShell. Both can list all variables and query specific ones, but the syntax is different.

In Command Prompt, wrap variable names in percent signs. Run echo %USERNAME% to print the USERNAME variable. To list all environment variables, type set and press Enter. The output shows every variable in alphabetical order, formatted as VAR=value. You can filter results by passing a prefix: set PATH lists all variables starting with PATH.

PowerShell uses the $Env: prefix. Run $Env:USERNAME to print the USERNAME variable. To list all environment variables, use Get-ChildItem Env:, which outputs a table with Name and Value columns. You can pipe the output to Where-Object for filtering: Get-ChildItem Env: | Where-Object {$_.Name -like "*PATH*"} shows all variables with PATH in the name.

Quick CMD and PowerShell steps:

  1. Open CMD (Win + R, type cmd, press Enter) or PowerShell (Win + S, search “PowerShell”).
  2. CMD single variable: echo %VARIABLE_NAME% (Example: echo %PATH%)
  3. CMD all variables: set
  4. PowerShell single variable: $Env:VARIABLE_NAME (Example: $Env:Path)
  5. PowerShell all variables: Get-ChildItem Env:
  6. PowerShell filter example: Get-ChildItem Env: | Where-Object {$_.Name -eq "USERNAME"}

Printing Environment Variables in Popular Programming Languages

8PBylihDSjWcYSM2nbbZAA

Most languages provide built in functions or modules to read environment variables. Below are copy and paste examples for printing a single variable and listing all variables in the most common languages.

Python

import os
print(os.getenv('HOME'))
print(os.environ.get('HOME'))
for k, v in os.environ.items():
    print(k, v)

Node.js

console.log(process.env.HOME);
Object.entries(process.env).forEach(([k, v]) => console.log(k, v));

Java

System.out.println(System.getenv("HOME"));
Map<String, String> env = System.getenv();
env.forEach((k, v) -> System.out.println(k + "=" + v));

PHP

echo getenv('HOME');
foreach ($_ENV as $key => $value) {
    echo "$key=$value\n";
}

Ruby

puts ENV['HOME']
ENV.each { |k, v| puts "#{k}=#{v}" }

Go

package main
import ("fmt"; "os")
func main() {
    fmt.Println(os.Getenv("HOME"))
    for _, e := range os.Environ() {
        fmt.Println(e)
    }
}

C

#include <stdio.h>
#include <stdlib.h>
int main() {
    printf("%s\n", getenv("HOME"));
    return 0;
}

Perl

print $ENV{'HOME'} . "\n";
while (my ($key, $value) = each %ENV) {
    print "$key=$value\n";
}

How to Print All Environment Variables (Full Listings and Filtering)

YdtrRZaHTCm0fzeNtPBAtA

Listing all environment variables is useful when you need to audit what’s loaded, debug missing values, or snapshot configuration before deployment. Each operating system and shell offers commands that dump the full environment.

On Linux and macOS, run env or printenv to list all exported environment variables. Both commands produce identical output in most shells. If you want shell variables and functions included, use set in bash or declare in zsh. These commands produce longer output because they include local and unexported variables.

On Windows, run set in Command Prompt or Get-ChildItem Env: in PowerShell. Both list every environment variable with name and value. PowerShell’s output is formatted as a table, making it easier to scan.

Commands for listing all environment variables:

  • Linux/macOS (environment only): env or printenv
  • Linux/macOS (with shell variables): set
  • Linux/macOS paged output: env | less or printenv | more
  • Windows CMD: set
  • Windows PowerShell: Get-ChildItem Env:

Pipe output to grep or more to manage long lists. Example: env | grep -i python shows only variables containing “python” (case insensitive).

Printing Specific Environment Variables (PATH, HOME, USER, Custom)

vD6AhHokRgCUByockYhlVg

Most debugging and configuration checks focus on a handful of common variables. PATH controls where the shell searches for executables. HOME points to the current user’s home directory. USER stores the logged in username. Custom variables hold application secrets, API endpoints, or runtime flags.

Print a single variable with echo or printenv. Run echo $PATH on Linux/macOS or echo %PATH% on Windows CMD. Always quote the variable expansion in scripts to preserve spaces: echo "$PATH". Use printf '%s\n' "$PATH" when the value might contain escape sequences or trailing newlines that echo would interpret.

Examples of printing specific environment variables:

  • Print PATH on Linux/macOS: echo $PATH
  • Print PATH on Windows CMD: echo %PATH%
  • Print PATH on Windows PowerShell: $Env:Path
  • Print HOME on Linux/macOS: printenv HOME
  • Print USER on Linux/macOS: echo $USER
  • Print custom variable: echo $MY_API_KEY
Variable Linux/macOS Windows PowerShell
PATH echo $PATH $Env:Path
HOME echo $HOME $Env:USERPROFILE
USERNAME echo $USER $Env:USERNAME

Scripted Printing of Environment Variables (Shell, CI/CD, Docker, Kubernetes)

laZl4A-pS8S9VJVNtFdHgw

Scripts and automation pipelines often print environment variables to verify configuration before running tests, builds, or deployments. Inline variable checks catch missing secrets or incorrect paths early, preventing runtime failures.

In shell scripts, use echo "$VARIABLE" or printf '%s\n' "$VARIABLE" to print values. Wrap the expansion in quotes to handle spaces and special characters. Example: echo "API endpoint is $API_ENDPOINT" prints a labeled value. To fail fast when a required variable is missing, use parameter expansion: echo "${REQUIRED_VAR?Variable not set}" exits with an error if REQUIRED_VAR is unset.

In Docker containers, run docker exec CONTAINER_NAME printenv to list all environment variables inside a running container. For a single variable, run docker exec CONTAINER_NAME printenv HOME. Docker Compose files let you print variables during build or runtime using ${VARIABLE} syntax in YAML.

In Kubernetes, use kubectl exec POD_NAME -- printenv to list variables inside a pod. For a specific variable, run kubectl exec POD_NAME -- printenv PATH. ConfigMaps and Secrets inject environment variables into pods, and printing them during debugging confirms they loaded correctly.

Copy and paste examples for scripts and containers:

  • Bash script safe print: echo "${VAR?Variable VAR not set}"
  • Docker print all vars: docker exec my_container printenv
  • Docker print single var: docker exec my_container printenv HOME
  • Kubernetes print all vars: kubectl exec my_pod -- printenv
  • Kubernetes print single var: kubectl exec my_pod -- printenv PATH
  • CI/CD (GitHub Actions): echo "Runner user: $USER"
  • CI/CD (GitLab CI): echo "Job ID: $CI_JOB_ID"

Secure Printing of Environment Variables (Avoiding Leaks)

y_50vk1dRZmKXWzqmSakfQ

Environment variables often hold secrets like API keys, database passwords, and OAuth tokens. Printing the full environment in logs, CI output, or error messages can leak these values to unauthorized users or public repositories.

Don’t run env or printenv in production logs. If you must print variables for debugging, filter out sensitive ones with grep. Example: env | grep -v SECRET | grep -v KEY hides lines containing SECRET or KEY. Better yet, print only the variables you need: echo $DEBUG_MODE instead of dumping everything.

In CI/CD pipelines, mask secrets by configuring your platform’s secret management. GitHub Actions, GitLab CI, and Jenkins automatically redact registered secrets in logs. Even with masking, avoid echo $SECRET_VAR because some CI runners log the command before executing it. Use a debug flag or conditional block to control when variables print.

Rules for safe environment variable printing:

  • Never run env or printenv in public CI logs or production output.
  • Use grep to exclude secrets: env | grep -v API_KEY | grep -v PASSWORD.
  • Print only the variables you need: echo "Config loaded from $CONFIG_PATH" instead of listing everything.
  • In scripts, check if a variable is set without printing its value: [ -z "$SECRET" ] && echo "Secret not set" || echo "Secret is set".

Final Words

In this guide we jumped straight into copy‑paste commands and quick methods for Linux, macOS, Windows, and common programming languages to print and inspect environment values.

You got essential one-liners (echo, printenv, set), detailed shell techniques, container/CI snippets, and safety rules to avoid leaking secrets.

When you need a quick check, use the cheat sheet to print environment variable across systems in seconds — it’s fast, repeatable, and safer than dumping everything. Small checks, fewer late‑night debugging sessions. You’re ready.

FAQ

Q: How do I print out or display an environment variable?

A: You print out or display an environment variable by running a shell command that expands it, e.g., echo $VAR or printenv VAR on Linux/macOS, or echo %VAR% on Windows.

Q: How to print env variables in cmd?

A: You print env variables in cmd using echo %VAR% for a single variable, or run set to list all environment variables; pipe to more when the output is long.

Q: What is the command to print all environment variables?

A: The command to print all environment variables depends on the shell: use printenv or env on Linux/macOS, set in Windows CMD, and Get-ChildItem Env: in PowerShell.

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