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

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 DISPLAYorenv | grep USER
Understanding Linux Environment Variables and Their Values

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

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:/binHOME=/home/userUSER=user
Listing Linux Environment Variables with the env Command

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.
- Run
envto list all exported variables - Run
env | grep VARIABLE_NAMEto filter by pattern - Run
env | moreto page through results - Run
env VAR=value commandto launch a program with a modified environment (example:env DISPLAY=:1 firefoxstarts Firefox on a different X display)
Showing Shell Variables and Functions Using set

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

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

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 $PATHecho $HOMEecho $USERecho $SHELL
Filtering and Searching Environment Variables on Linux

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.
- Case-insensitive match:
printenv | grep -i langfinds LANG, LANGUAGE, and related locale variables - Anchored variable search:
env | grep '^HOME='returns exactly the HOME variable - 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

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.
