How To List All Environment Variables In Linux : Linux Environment Variables Display

Environment variables in Linux store system-wide settings that applications can reference at runtime. If you’ve ever wondered how to list all environment variables in linux, you’re in the right place. These variables control everything from your shell prompt to where programs look for files. Knowing how to view them is essential for troubleshooting, scripting, or just understanding your system better.

In this guide, I’ll show you multiple ways to list environment variables, explain what they mean, and give you practical examples. You’ll learn commands like printenv, env, and set, plus how to filter and export them. Let’s get started.

How To List All Environment Variables In Linux

The most direct way to see every environment variable is using the printenv command. Open your terminal and type:

printenv

This outputs a long list of key-value pairs, one per line. Each line shows the variable name followed by an equals sign and its value. For example, HOME=/home/username tells you your home directory.

If you want a cleaner view, pipe the output to less or more:

printenv | less

This lets you scroll through the list page by page. Press q to exit.

Using The Env Command

Another simple method is the env command. Running env without arguments shows all environment variables:

env

It works almost identically to printenv, but there’s a subtle difference. env can also run a program with modified environment variables, while printenv is purely for display. For listing, either is fine.

To see a specific variable, pass its name to printenv:

printenv PATH

This shows only the value of the PATH variable. Handy when you don’t want to scan the whole list.

Using The Set Command

The set command lists not only environment variables but also shell variables and functions. Type:

set

You’ll get a massive output. To filter only environment variables, combine set with grep:

set | grep -E '^[A-Z_]+='

This pattern matches lines starting with uppercase letters or underscores followed by an equals sign. It’s a quick way to isolate environment variables from the noise.

Note that set includes variables that are local to the current shell, not exported. For a true list of exported environment variables, stick with printenv or env.

Checking Specific Variable Groups

Sometimes you need to list variables related to a certain topic. For example, to see all variables containing “HOME”:

env | grep HOME

Or to find variables set by your system’s locale:

printenv | grep -i lang

The -i flag makes the search case-insensitive. This is useful when you’re not sure about the exact variable name.

Listing Variables In A Script

When writing bash scripts, you might want to list environment variables programmatically. Use a loop with printenv:

#!/bin/bash
for var in $(printenv | cut -d= -f1); do
    echo "Variable: $var = ${!var}"
done

This iterates over each variable name, then uses indirect expansion (${!var}) to get its value. It’s a bit advanced but very flexible.

Alternatively, use env in a while loop:

env | while IFS='=' read -r name value; do
    echo "$name = $value"
done

Both methods work, but the first is more efficient for large lists.

Understanding The Output

Environment variables follow a standard format: NAME=VALUE. The name is usually uppercase with underscores. Common variables include:

  • HOME: Your user’s home directory.
  • PATH: Directories where the shell looks for executables.
  • USER: Your current username.
  • SHELL: The path to your default shell.
  • LANG: System language and locale settings.

Values can be strings, paths, or numbers. Some variables like PATH contain multiple paths separated by colons.

What About Shell Variables?

Shell variables are different from environment variables. Shell variables are local to the current shell session and aren’t passed to child processes. To see both, use set without filtering. To see only shell variables, subtract environment variables:

comm -23 <(set -o posix; set | sort) <(env | sort)

This is a bit hacky but works. The comm command compares two sorted lists and shows lines unique to the first.

Exporting And Modifying Variables

Once you know how to list variables, you might want to change them. To export a new variable:

export MY_VAR="hello"

To make it permanent, add it to your shell’s startup file like ~/.bashrc or ~/.profile. After editing, source the file:

source ~/.bashrc

To remove a variable, use unset:

unset MY_VAR

This deletes it from the current session. For permanent removal, delete the line from your startup file.

Listing Variables From A File

Sometimes environment variables are stored in files like /etc/environment or ~/.pam_environment. To view them:

cat /etc/environment

Note that these files use a different format—no export keyword needed. They’re read by the system at login.

You can also check /proc/1/environ to see the initial environment of the init process:

cat /proc/1/environ | tr '\0' '\n'

This file uses null bytes as separators, so we convert them to newlines for readability.

Practical Use Cases

Knowing how to list environment variables helps in many scenarios:

  • Debugging: Check if a variable is set correctly.
  • Scripting: Ensure required variables exist before running a script.
  • Security: Verify sensitive variables like API_KEY aren’t exposed.
  • System administration: Understand how your system is configured.

For example, if a program can’t find a library, check LD_LIBRARY_PATH:

printenv LD_LIBRARY_PATH

If it’s empty, the library might be in a standard location or the variable needs to be set.

Filtering With Grep

Use grep to find variables matching a pattern. For instance, to list all variables related to Java:

env | grep -i java

Or to see variables with “proxy” in their name:

printenv | grep -i proxy

This is faster than scrolling through hundreds of lines.

Common Mistakes

Beginners often confuse printenv and set. Remember:

  • printenv and env show only environment variables.
  • set shows everything, including shell functions.

Another mistake is thinking that modifying a variable in a script affects the parent shell. It doesn’t—unless you source the script or use export in the right context.

Also, don’t forget that environment variables are case-sensitive. PATH and path are different.

Advanced Techniques

For power users, you can list variables with their values sorted alphabetically:

printenv | sort

Or count how many variables you have:

printenv | wc -l

To see only variable names without values:

printenv | cut -d= -f1

This is useful for scripting or comparing environments.

Using Awk For Formatting

If you want a more readable output, use awk:

printenv | awk -F= '{print $1 " -> " $2}'

This replaces the equals sign with an arrow for clarity. You can customize the separator as needed.

Environment Variables In Different Shells

The commands above work in bash, zsh, and most POSIX shells. In csh or tcsh, use setenv instead:

setenv

To list all variables in csh:

printenv

Still works, but the syntax for setting variables differs. Stick with bash if you’re learning.

Checking For Empty Variables

Sometimes a variable exists but has no value. To find these:

printenv | grep '=$'

This shows variables with an empty value. They can cause issues in scripts, so it’s good to know they’re there.

Security Considerations

Environment variables can contain sensitive data like passwords or API keys. Be cautious when listing them in shared terminals or logs. Use printenv only in trusted environments.

If you need to share your environment for debugging, redact sensitive values first:

printenv | sed 's/=.*/=REDACTED/'

This replaces all values with “REDACTED” while keeping variable names visible.

Conclusion

Now you know multiple ways to list environment variables in Linux. Start with printenv for a quick overview, use env for scripting, and set when you need everything. Filter with grep to find specific variables, and remember to check your shell’s documentation for differences.

Practice these commands in your terminal. The more you use them, the more natural they’ll become. Environment variables are a core part of Linux, and mastering them will make you a more effective user and administrator.

Frequently Asked Questions

What Is The Difference Between Printenv And Env?

Both list environment variables, but env can also run commands with modified environments. For listing, they’re interchangeable. printenv is slightly more readable.

How Do I List Only Exported Environment Variables?

Use printenv or env without arguments. These show only exported variables. Shell variables local to the session are not included.

Can I List Environment Variables From A Different User?

Yes, with sudo. For example: sudo -u otheruser printenv. This shows the environment of that user, but only if you have permissions.

Why Does Set Show More Than Printenv?

set includes shell variables, aliases, and functions. printenv only shows variables passed to child processes. Use set when you need a complete picture of the shell state.

How Do I Save The List To A File?

Redirect the output: printenv > env_vars.txt. This saves the list for later analysis or comparison.