View All Environment Variables Linux: Commands and Syntax

Published:

Think you know what’s in your shell’s environment?

If you need to view all environment variables on Linux, this intro gives the exact commands and quick syntax you’ll run in your terminal.
You’ll learn when to use printenv or env for exported vars, when set is needed for shell-local stuff, and how to read /proc/$$/environ for the kernel’s raw snapshot.
Copy-paste examples, quick filters, and common gotchas help you find the value fast.
No fluff, just commands you can run now.

Essential Commands to View All Environment Variables on Linux

VA-QCssxQXe31QTFizRrTA

Three commands do most of the heavy lifting when you need to list environment variables on Linux. printenv shows exported environment variables, the ones child processes inherit. env displays those same exported variables and lets you tweak the environment for a single command. set dumps everything: shell variables, environment variables, shell functions. If you want a quick snapshot of what subprocesses will see, reach for printenv or env. Need to debug shell-local state that isn’t exported? That’s when set earns its keep.

Copy these into your terminal and watch what comes back. env gives you simple lines like PATH=/usr/local/bin:/usr/bin:/bin and HOME=/home/username. Run env | wc -l and you might see 84, meaning 84 exported variables live in your session. printenv HOME spits out /home/username, or nothing if that variable doesn’t exist. set floods your screen with hundreds of lines, including shell functions and non-exported variables like PS1 and TMOUT. Want exported variables with shell syntax? Try export -p or declare -x. You’ll get lines like declare -x PATH="/usr/bin:/bin".

The /proc/$$/environ file holds a null-separated snapshot of your current process’s environment exactly as the kernel tracks it. Convert those nulls into newlines with tr '\0' '\n' < /proc/$$/environ and you get a readable list. This bypasses shell layers entirely, showing the raw process environment. Handy when you suspect shell functions or aliases are masking variables.

  • printenv prints all exported environment variables or a single named one
  • env lists exported variables and can run commands with modified environments
  • set displays shell variables, exported variables, and functions
  • export -p shows exported variables with export syntax
  • declare -x lists exported variables in Bash declaration format
  • tr ‘\0’ ‘\n’ < /proc/$$/environ reads the kernel’s null-separated environment for the current shell process

Understanding Linux Environment Variables and Their Structure

a7iH5uc7T4StvJBO_gRK0g

Environment variables are key-value pairs living in shell and kernel process memory. When you spawn a child process from the shell, whether it’s a script, an editor, or another program, that child inherits every exported variable. Shell variables stick to the current session and disappear when you close the shell unless you export them. Exporting marks a shell variable for inheritance, turning it into an environment variable visible to subprocesses. printenv or env only shows exported variables. set reveals the whole picture, including local shell settings and function definitions.

Common environment variables shape system behavior and user preferences. PATH contains colon-separated directories the shell searches when you run a command: /usr/local/bin:/usr/bin:/bin. HOME points to your user directory, /home/username. SHELL identifies which shell binary you’re running, like /bin/bash. LANG controls your locale and language, often en_US.UTF-8. TERM defines your terminal type, usually xterm-256color. These get set at login and stick around unless you override them in config files or during runtime.

Variable Description Inherited?
PATH Colon-separated directories searched for commands Yes
HOME User’s home directory path Yes
SHELL Path to the user’s login shell Yes
LANG System locale and language setting Yes
TERM Terminal type identifier Yes

Filtering, Sorting, and Inspecting Linux Environment Variables

cSOmYFGMQtKCc-7JQijo6g

Production servers often export 80 or 100 variables. Scrolling through raw env output eats time when you’re hunting for one specific setting or comparing environments between servers. Filtering cuts the list down to what matters. Sorting groups related variables alphabetically. Counting helps you spot when a deployment script added unexpected entries or when a config change bloated the environment.

Pipe env or printenv into grep, sort, or wc to slice and analyze the output. grep matches patterns and filters lines. sort arranges keys alphabetically for easier comparison. wc -l counts how many variables exist. String them together to answer questions fast. How many variables mention ‘user’? What’s the exact value of LANG?

  1. Case-insensitive matchenv | grep -i TERM finds TERM, COLORTERM, or any variable name or value with “term” regardless of case.
  2. Exact-name matchprintenv | grep '^LANG=' shows only the line starting with LANG=, dodging partial matches in values.
  3. Count matchesenv | grep -i user | wc -l tells you how many variables or values contain “user”.
  4. Sorted outputenv | sort or printenv | sort orders variables alphabetically, grouping similar prefixes.
  5. Filter multiple variablesenv | grep -E '^(PATH|HOME|SHELL)=' displays only PATH, HOME, and SHELL on separate lines.

Viewing Specific Linux Environment Variables and Their Values

jKy8GtMWRJ2tcALnc6xKaw

When you just need one variable’s value, like checking if PATH includes a custom directory or confirming HOME is correct, use echo $VARIABLENAME or printenv VARIABLENAME. Both print the value to stdout. echo $HOME might return /home/username. printenv HOME does the same. If the variable doesn’t exist, echo prints a blank line and printenv prints nothing. The dollar-sign syntax ($VAR) is standard shell substitution. printenv is a standalone command that doesn’t need the dollar sign.

Environment variable names are case-sensitive and usually uppercase: PATH, USER, HOME, SHELL. Lowercase names typically mark shell-local or temporary variables. Mixed cases won’t match. echo $path returns nothing if only PATH exists. Stick to uppercase for exported variables and double-check names when values go missing.

  • PATH search path for executables; example: echo $PATH returns /usr/local/bin:/usr/bin:/bin
  • HOME user home directory; example: printenv HOME returns /home/username
  • USER current username; example: echo $USER returns username
  • SHELL login shell; example: printenv SHELL returns /bin/bash

System-Wide vs User-Specific Environment Variables on Linux

jQyzhFvcQcqoEeBlaG_Img

Environment variables can be defined system-wide, affecting every logged-in user, or user-specific, applying only to your account. System-wide files live in /etc and need root or sudo to edit. User files sit in your home directory and you control them directly. Knowing which file sets a variable helps you override unwanted defaults or trace where a surprising value came from.

System-wide variables get set in /etc/environment (simple KEY=value pairs, no export needed), /etc/profile (sourced by login shells), /etc/profile.d/*.sh (modular scripts for package-provided variables), and /etc/bash.bashrc (read by interactive non-login Bash shells). User-level variables live in ~/.profile (POSIX-compliant login shells), ~/.bash_profile or ~/.bash_login (Bash login shells, first found wins), ~/.bashrc (interactive non-login Bash), and ~/.zshrc (Zsh). Ubuntu, Debian, CentOS, and Red Hat all respect this hierarchy, though default user skeleton files differ slightly.

To make a variable stick for your account, add export VARNAME=value to ~/.bashrc and run source ~/.bashrc to apply it without logging out. For a login-only variable, edit ~/.profile or ~/.bash_profile. System administrators setting a variable for all users would add it to /etc/environment or a script in /etc/profile.d/, then ask users to re-login or source the file manually. Check which file your shell sources by running echo $BASH_VERSION and consulting man bash to confirm login vs non-login behavior.

Advanced Techniques to View Environment Variables in Linux Processes

LlgG6dm5RbGG28bAbywDPA

You can inspect the environment of any running process or temporarily modify variables for a single command without cluttering your session. These techniques matter when debugging daemons, comparing environments between parent and child processes, or testing config changes in isolation.

Read your current shell’s environment directly from the kernel with tr '\0' '\n' < /proc/$$/environ. The special file /proc/$$/environ represents your shell’s process ID ($$) and stores variables as null-separated strings. tr translates null bytes (\0) into newlines, producing output like env but sourced from the kernel’s process table instead of shell internals. Run a command with modified variables using env VAR=value command_to_run. For example, env DEBUG=1 ./myscript.sh sets DEBUG=1 only for that script invocation, leaving your shell environment untouched.

Viewing Another Process’s Environment

Every Linux process exposes its environment in /proc/<pid>/environ, where <pid> is the process ID. Run ps aux | grep processname to find the PID, then sudo tr '\0' '\n' < /proc/1234/environ (swap 1234 with the actual PID) to dump that process’s variables. You need read permission on the file. Your own processes are readable, but inspecting another user’s process or a system daemon requires sudo or root. Be careful. Environment variables can hold secrets like API keys or database passwords, and reading another user’s environment may expose sensitive data.

Common Issues When Viewing Environment Variables on Linux

PShDT9beTPel_XpomtjIHw

Variables sometimes don’t appear when you expect them, or they show up in interactive sessions but vanish inside scripts. These problems usually come down to scope, export status, or which startup file the shell sourced. Understanding the difference between login shells, non-login shells, and non-interactive script execution saves hours of confusion.

A variable missing from printenv but present in set means it’s a shell variable that was never exported. Export it with export VARNAME and it’ll appear in child processes. Scripts run by cron, systemd, or other non-interactive contexts don’t source ~/.bashrc by default. Bash only reads ~/.bashrc in interactive non-login shells. If you rely on a variable defined in ~/.bashrc, either source it explicitly at the top of your script (source ~/.bashrc) or move the definition to ~/.profile (login shells) or set it in the script itself.

Conflicting definitions across multiple files can override each other in unexpected order. Bash reads /etc/profile, then the first found of ~/.bash_profile, ~/.bash_login, or ~/.profile for login shells, and /etc/bash.bashrc then ~/.bashrc for non-login interactive shells. A variable set in /etc/profile might get overridden in ~/.bashrc, or the other way around. Search for duplicate assignments with grep -r "VARNAME" ~/.bash* /etc/profile /etc/bash.bashrc to locate every definition and resolve conflicts. Check your PATH formatting carefully. Missing colons, trailing slashes, or nonexistent directories break command lookup. Run echo $PATH and verify each colon-separated entry with ls.

  • Variable not exported shell variable exists but printenv doesn’t show it; fix with export VARNAME
  • Wrong startup file edited added export to ~/.bash_profile but shell is non-login; move definition to ~/.bashrc
  • Conflicting definitions across files variable set in multiple configs; search with grep -r and consolidate
  • PATH formatting issues missing colons, extra slashes, or nonexistent directories; inspect with echo $PATH and ls each entry

Final Words

in the action, we covered the fastest ways to inspect your environment: printenv, env, set, export -p, declare -x, and /proc/$$/environ.

You got copy/paste examples, filtering with grep and sort, echo $VAR for single values, and the difference between system and user files that make variables persistent.

If something’s missing, check exported vs shell variables, the right startup file, or inspect /proc for process-level values. You can now view all environment variables linux quickly and safely—go try it.

FAQ

Q: How do you display or list environment variables in Linux, and how can you view both shell and environment variables?

A: You can display or list environment variables in Linux with env or printenv (lists exported), use set to show shell variables and functions, echo $VAR for one value, and tr ‘\0’ ‘\n’ < /proc/$$/environ to view the raw process environment.

Q: What does $_ mean in bash?

A: The $_ variable in bash holds the last argument of the previous command, useful for quickly reusing file paths; in some contexts it can also contain the shell or script pathname at startup.

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