List Environment Variables in Linux: Commands to Display All Settings

Published:

Ever spent 10 minutes scrolling terminal output to find one PATH entry?
If so, you’re not alone.
This post gives the quick commands to list environment variables in Linux—printenv, env, set, declare, and echo—and when to use each.
You’ll learn how to dump every exported variable, check shell-local values, search with grep, and confirm a single variable in seconds.
By the end you’ll know which command matches each debugging need and how to make changes persist across sessions.

Quick Commands to List Environment Variables

A9ZJ9dlfSmK7YNq2IDhzDw

To list environment variables in Linux, run printenv in your terminal. For filtering or viewing shell-local variables, use env or set.

View a single variable with printenv VARIABLE_NAME or echo $VARIABLE_NAME. When you need to search through dozens of variables, pipe the output to grep. Ever spent 10 minutes scrolling through terminal output looking for one PATH entry? grep solves that.

For quick filtering without comparing command behaviors, use env | grep PATH to find variables matching a pattern. Each command produces slightly different outputs, but all three handle the basic task of displaying variable data.

  • List all variables with printenv
  • List all variables with env
  • List shell variables with set
  • View specific variable with printenv HOME
  • View specific variable with echo $HOME
  • Filter results using printenv | grep DISPLAY or env | grep USER

Understanding Linux Environment Variables and Their Values

EOX7d79gRC6NUZYO0y7AUg

Environment variables are named values that control program behavior and are inherited by child processes. A typical Linux system contains about 50 environment variables, though the count varies by distribution and desktop environment. Variable names follow uppercase conventions (PATH, HOME, USER) and represent user information, session data, or system configuration.

Global environment variables are visible to all processes started from the current shell. Local shell variables exist only in the current shell session and aren’t exported to child processes. Session variables persist for the duration of your login, while shell variables reset when you close the terminal. Tools like printenv show only exported variables, while set displays all shell data including functions.

Variable Description Example Value
PATH Colon-separated directories searched for executable commands /usr/local/bin:/usr/bin:/bin
HOME Current user’s home directory path /home/user
USER Username of the current account user
SHELL Default shell program for the user /bin/bash

Using printenv to Display Linux Environment Variables

4gVXJyooQm-bw-0GqLgEdg

Running printenv with no arguments lists all exported environment variables in your current shell. Unlike env, which was designed to run commands with modified environments, printenv is purpose-built for displaying variable data. It’s the cleanest option when you just want to see what’s set.

For large outputs, pipe to less for scrolling: printenv | less. To find specific variables, use grep: printenv | grep HOME. When you provide variable names as arguments (printenv HOME SHELL), printenv displays only those values without dumping the entire environment.

  • PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  • HOME=/home/user
  • USER=user

Listing Linux Environment Variables with the env Command

C1JeX3awQv2M_DpGplNmoQ

The env command was built to run programs with modified environments, but when called with no arguments it lists all exported environment variables as a side effect. Developers commonly use env in scripts to locate the correct interpreter. The #!/usr/bin/env python3 shebang is a classic example.

Filter the output with grep to narrow results: env | grep PATH shows only variables containing “PATH.” For paging through long lists, use env | more to scroll one screen at a time.

  1. Run env to list all exported variables
  2. Run env | grep VARIABLE_NAME to filter by pattern
  3. Run env | more to page through results
  4. Run env VAR=value command to launch a program with a modified environment (example: env DISPLAY=:1 firefox starts Firefox on a different X display)

Showing Shell Variables and Functions Using set

TZy11GAtTEWr3Pce8fPE7w

Running set with no options prints names and values of all shell variables in the current shell, including both exported environment variables and shell-local variables like PS1 (your command prompt) and TMOUT (shell timeout). It also shows defined functions, making the output more verbose than printenv or env.

The set output is sorted by locale and formatted so you can reuse it as input to reset variables later. Use set when debugging shell state, checking function definitions, or tracking down variables that aren’t exported but still affect shell behavior.

Declaring and Inspecting Variables with the declare Command

xqPLL3qsS-SuUWRMG4In6g

The declare built-in displays shell variables when run with no arguments. It shows variable types and attributes (like whether a variable is read-only or an array), but unlike set it doesn’t list functions unless you add specific flags.

Developers reach for declare when writing automation scripts or debugging variable assignments. It’s useful for confirming variable scope and checking attributes before you modify or export a value. Before exporting USER_CONFIG, I run declare | grep USER_CONFIG to make sure I’m not overwriting something critical.

Retrieving Specific Linux Environment Variables with echo

ZK5owOTFTa6YbjqW2sae9w

Use echo $VARIABLE_NAME to print the value of a single variable without dumping your entire environment. This syntax is fast when you already know the variable name and just need to check its current value.

Common variables you’ll check with echo include PATH (to verify binary search paths), HOME (to confirm the user’s home directory), USER (to see the current username), and SHELL (to identify the default shell). The $ prefix tells the shell to expand the variable name into its value.

  • echo $PATH
  • echo $HOME
  • echo $USER
  • echo $SHELL

Filtering and Searching Environment Variables on Linux

Zc7kj87ISdmVADRgiYeUgA

Pipe printenv or env to grep to filter large outputs. For case-insensitive searches, add the -i flag: printenv | grep -i display finds DISPLAY, display, or any mixed-case match. Anchoring the search pattern with ^ ensures you match variable names at the start of each line: printenv | grep '^PATH=' returns only the PATH variable, not variables like MANPATH that contain “PATH” later in the name.

Use grep -E with multiple patterns to search for several variables at once. When you’re troubleshooting locale issues or tracking down which variables a script inherits, filtering saves you from scrolling through 50 lines of output.

  1. Case-insensitive match: printenv | grep -i lang finds LANG, LANGUAGE, and related locale variables
  2. Anchored variable search: env | grep '^HOME=' returns exactly the HOME variable
  3. Multi-pattern regex: printenv | grep -E -w 'HOME|PWD|USER' shows HOME, PWD, and USER in one pass

Sources, Persistence, and Exporting of Linux Environment Variables

YxV-d2k0QGSWvMkjefbcFQ

Run export VAR=value to make a variable available to child processes for the current session. When you close the terminal, the variable disappears. To persist changes across sessions, add export lines to ~/.bashrc (for interactive Bash shells) or ~/.profile (for login shells and other shell types).

Edit ~/.bashrc to set variables like HISTSIZE (controls how many commands Bash remembers) or to modify PATH permanently. After saving the file, run source ~/.bashrc to reload it without logging out. The changes take effect in new terminals automatically.

System-wide settings live in /etc/environment, /etc/profile, or scripts inside /etc/profile.d/. Which file your distribution uses varies. Debian-based systems often use /etc/environment, while Red Hat-based systems rely on /etc/profile.d/ drop-in scripts. Check your distro’s documentation if you need to set variables for all users.

  • ~/.bashrc — per-user, interactive Bash sessions
  • ~/.profile — per-user, login shells (read by sh-compatible shells)
  • /etc/environment — system-wide, simple VAR=value format (no shell expansion)
  • /etc/profile — system-wide, executed by login shells
  • /etc/profile.d/*.sh — system-wide drop-in scripts sourced by /etc/profile

Final Words

in the action we ran through the fastest commands to inspect environment variables: printenv, env, set, declare, and echo, plus quick filtering and persistence tips.

Quickly: printenv/env show exported vars, set and declare reveal shell-local vars and functions, echo prints single values, and grep helps narrow output. Use export and shell rc files to persist changes.

If you need to list environment variables in linux, start with printenv or env, then grep or echo to narrow results. You’ll be able to check and fix env settings fast.

FAQ

Q: How do you check or display the list of environment variables in Linux?

A: To check or display the list of environment variables in Linux, run printenv or env to list exported variables, or set to show shell-local variables; use printenv NAME or echo $NAME for single values.

Q: What does $_ mean in bash?

A: The $_ variable in bash holds the last argument of the previous command in interactive shells; in scripts it typically contains the last word of the previous simple command, useful for quick re-use.

Q: Are there environment variables in Linux?

A: Linux has environment variables that store session and system information, are inherited by child processes, and common examples include PATH, HOME, USER, and SHELL.

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