The Linux `env` command prints the environment variables available in your current session, along with their values. It’s a clean way to inspect shell settings or check what a script can access. While `printenv` can also display them, `set` includes shell variables and functions too.

Multiple Choice

How do you list environment variables in Linux?

Listing environment variables in Linux can be done using several commands, each offering slightly different functionality. The correct answer is the command that most commonly displays all current environment variables along with their values. When you use the command `env`, it prints out a list of all the environment variables currently set in your session. This command is particularly useful for viewing the environment portion of your shell and is often used in scripts to check what environment settings are available at a certain point. While other commands like `printenv` and `set` can also show environment variables, `env` is widely recognized for its straightforward purpose of displaying the environment without extraneous information. The `set` command can show all shell variables and functions, which includes local variables that are not necessarily environment variables, making it less focused than `env` for this specific task. The command `showenv` is not a standard command in most Linux distributions for listing environment variables, which is why it is not the correct choice. Overall, using `env` provides a clean and clear output of all environment variables, making it the best answer for comprehensively listing them in Linux.

How to List Environment Variables in Linux

A Linux command often behaves differently depending on the environment in which it runs. A program may look for files in one set of folders, use a particular language, or save temporary data in a chosen location. These settings come from environment variables: named values that a shell passes to programs it starts.

To display the environment variables available in your current session, run:


env

The command prints each variable and its value, usually one per line. You might see entries such as HOME, PATH, LANG, and SHELL. It’s a simple command, but it offers a useful snapshot of the conditions surrounding your shell and the programs launched from it.

What does env show?

Try running env in a terminal. The output may look something like this:


HOME=/home/sam

LANG=en_US.UTF-8

PATH=/usr/local/bin:/usr/bin:/bin

SHELL=/bin/bash

USER=sam

Your results will vary. Linux systems and user accounts don’t all have the same settings, and some variables may be added by desktop software, login scripts, or applications. The list can also be fairly long, so it may scroll past quickly.

Each line has a name, an equals sign, and a value. In PATH, for example, the value is a colon-separated list of directories. When you type a command such as ls, the shell checks those directories to find the program file. If a directory isn’t in PATH, the command may still exist, but the shell might not find it by name.

Other variables tell programs where a user’s home directory is (HOME), which language or regional settings to use (LANG), or which shell is in use (SHELL). They act a little like notes passed from the shell to its children: small pieces of context that can make a real difference.

Why are they called “environment” variables?

A shell can hold many kinds of values, but an environment variable is one that has been marked for export to programs started by that shell. That distinction matters.

For example, a shell variable created like this is available in the current shell:


color=blue

But a program launched from that shell won’t necessarily receive it. To pass it along, export the variable:


export color=blue

You can then inspect it with:


env

The variable should appear as color=blue. You can also confirm its value with:


echo "$color"

These commands answer slightly different questions. env displays the environment passed to the command. echo prints the value of a particular shell expression. If you create a shell variable without exporting it, echo "$color" can still show its value, even though env may not list it.

That’s why a variable can seem to “exist” in one place but not another. Shells keep their own working values, while exported environment variables travel to child processes.

env, printenv, and set: what’s the difference?

Linux offers more than one way to inspect variables, and the commands overlap. Still, each has a slightly different focus.

env: show the current environment

With no arguments, env lists the environment variables available to the command:


env

It can also run another command with a changed environment. For example:


env EDITOR=nano some-command

Here, some-command runs with EDITOR set to nano. This setting applies to that command and its child processes; it doesn’t permanently change the value in your shell.

You can also launch a command with an empty environment:


env -i some-command

That can help when checking whether a program depends on settings inherited from the terminal. Use care, though: a stripped-down environment may omit values the program expects, such as PATH or HOME.

printenv: print variables by name

printenv is also designed to display environment variables. Run it without an argument to list them:


printenv

To show one value, provide its name:


printenv HOME

This makes printenv handy for quick checks. It’s often clearer than scanning the full list when you already know which variable matters. If a name isn’t present in the environment, printenv generally produces no value for it.

set: show shell state

In Bash, set without arguments displays shell variables and other shell state. That includes values that haven’t been exported, as well as environment variables. Depending on the shell, its output may also include functions and settings.

So set can show more than the process environment. It’s useful when you want to inspect what the shell knows, but it can produce a much larger, less focused display.

The distinction is easy to remember: use env or printenv to look at the environment; use set when you want a broader view of the shell itself.

Finding one variable without scrolling

A full listing is useful when you’re exploring, but it’s not always the fastest way to find one value. You can ask for a specific variable with printenv:


printenv PATH

Or use shell expansion:


echo "$PATH"

The quotation marks are a good habit. They help preserve spaces and other special characters in a value, should those appear. For a quick look at several related values, shell tools such as grep can filter the output:


env | grep -E '^(HOME|LANG|PATH|SHELL)='

This sends the output of env through a pipe to grep, which keeps lines that match the listed names. The ^ marks the start of a line, and the equals sign helps avoid matching a different variable that merely contains the same text.

One caution: don’t assume that every variable is harmless to display or share. Environment values can contain paths, service settings, or sensitive information such as tokens. If you’re copying terminal output into a report or online post, scan it first.

Setting a variable for a shell session

You can create an environment variable for the current shell with export:


export PROJECT_DIR="$HOME/projects"

Now, commands launched from that shell can receive PROJECT_DIR. Check it with either:


env | grep '^PROJECT_DIR='

or:


printenv PROJECT_DIR

The value usually lasts only for the current shell session and the processes it starts. Close the terminal, and the setting generally disappears. To have a variable set automatically in future sessions, shell configuration files may be involved, such as ~/.bashrc for Bash. The right file depends on the shell and how you start it, so a little care helps avoid confusing startup behavior.

Also note that variable names are case-sensitive. PATH, Path, and path are three different names on Linux. Uppercase names are common for environment variables, though the system doesn’t require that style.

A few small troubleshooting clues

Environment variables often explain problems that otherwise seem mysterious. If a command works in one terminal but not another, compare PATH. If an application starts with the wrong language or date format, inspect LANG and related locale variables. If a tool saves files somewhere unexpected, check values such as HOME or application-specific settings.

For a quick snapshot, use:


env

For a single value, try:


printenv VARIABLE_NAME

And when you need to see the shell’s broader collection of variables and functions, use:


set

These commands aren’t interchangeable in every detail, but together they make Linux’s variable system much easier to understand. Start with env when you want a clean view of what the shell is passing along. Often, that one glance is enough to spot the setting that’s shaping a program’s behavior.