How to echo environment variable

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To echo an environment variable in most command-line shells, you use the `echo` command followed by the variable name prefixed with a dollar sign (`$`). For example, to display the value of the `PATH` variable, you would type `echo $PATH`.

Key Facts

What are Environment Variables?

Environment variables are dynamic named values that can affect the way running processes will behave on a computer. They are part of the operating system's environment in which a process runs. Think of them as a set of key-value pairs that store information about the system and its configuration. These variables are often used to store paths to executable files, user-specific settings, temporary directory locations, and other crucial information that applications need to function correctly. They provide a flexible way to configure software without modifying the code itself.

Why Echo an Environment Variable?

Echoing an environment variable is a fundamental operation for several reasons:

How to Echo Environment Variables (Common Operating Systems)

Linux and macOS (Bash, Zsh, etc.)

In Unix-like systems, the most common way to echo an environment variable is by using the `echo` command followed by the variable name, which must be prefixed with a dollar sign (`$`).

Syntax:

echo $VARIABLE_NAME

Examples:

You can also echo multiple variables at once:

echo "User: $USER, Home: $HOME"

If you want to see the exact string including the dollar sign, you can enclose the variable name in curly braces or escape the dollar sign:

echo "${VARIABLE_NAME}"# orecho \$VARIABLE_NAME

Windows (Command Prompt and PowerShell)

Windows has a slightly different syntax for accessing environment variables.

Command Prompt (cmd.exe)

In the traditional Command Prompt, environment variables are enclosed in percent signs (`%`).

Syntax:

echo %VARIABLE_NAME%

Examples:

PowerShell

PowerShell uses a syntax similar to Linux/macOS, but the variables are accessed through the automatic variable `$env:`.

Syntax:

echo $env:VARIABLE_NAME

Examples:

PowerShell also supports the `%VARIABLE_NAME%` syntax for compatibility with `cmd.exe`, but using `$env:` is the native and recommended approach.

Setting Environment Variables (Temporarily)

While this article focuses on echoing, it's worth noting how variables are often set. Setting a variable temporarily means it will only exist for the current shell session or command.

Linux/macOS

export MY_VARIABLE="some_value"echo $MY_VARIABLE

Windows Command Prompt

set MY_VARIABLE=some_valueecho %MY_VARIABLE%

Windows PowerShell

$env:MY_VARIABLE = "some_value"echo $env:MY_VARIABLE

Important Considerations

Sources

  1. Environment variable - WikipediaCC-BY-SA-4.0
  2. ECHO - SS64 Command Linefair-use
  3. About Environment Variables - PowerShell | Microsoft Learnfair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.