Skip to main content

Command Palette

Search for a command to run...

How to Display Environment Variable in Linux

Published
6 min readView as Markdown

When working with Linux, environment variables are essential. They store information that your system and applications use to run smoothly. You might want to see what these variables are to troubleshoot, configure software, or just understand your system better. Luckily, Linux offers easy ways to display environment variables.

In this article, I’ll guide you through different methods to display environment variables in Linux. Whether you’re a beginner or have some experience, you’ll find clear steps and tips to help you check these variables quickly. Let’s dive in and explore how you can view environment variables on your Linux system.

What Are Environment Variables in Linux?

Environment variables are dynamic values stored in your system’s memory. They influence how processes run and how software behaves. For example, variables like PATH tell your system where to find executable files, while HOME points to your user directory.

These variables can be set globally for all users or locally for a single session. They help customize your environment without changing program code. Understanding and displaying these variables is key to managing your Linux system effectively.

How to Display All Environment Variables

If you want to see every environment variable currently set in your session, Linux provides a few commands that make this easy.

  • printenv: This command prints all or specific environment variables.
  • env: Similar to printenv, it lists all environment variables.
  • set: Shows all shell variables, including environment and shell-specific ones.

Using printenv

Simply type printenv in your terminal and press Enter. You’ll see a list of environment variables and their values.

printenv

If you want to check a specific variable, add its name after the command:

printenv PATH

This will display the value of the PATH variable only.

Using env

The env command is another way to list environment variables:

env

It outputs all environment variables in your current session. You can also use it to run commands with a modified environment.

Using set

The set command shows all shell variables, including environment variables and shell functions:

set

This list is usually longer because it includes local shell variables, not just environment variables.

How to Display a Specific Environment Variable

Sometimes, you only need to check one variable. There are simple ways to do this.

Using echo

The most common method is to use the echo command with a dollar sign $ before the variable name:

echo $HOME

This prints the value of the HOME environment variable, which usually points to your user directory.

Using printenv

As mentioned earlier, printenv can also display a single variable:

printenv USER

This shows the current username stored in the USER variable.

How to Display Environment Variables in Different Shells

Linux supports various shells like Bash, Zsh, and Fish. Displaying environment variables works similarly but with slight differences.

Bash Shell

Bash is the most common shell. Use printenv, env, or echo as explained above.

Zsh Shell

Zsh users can use the same commands. Additionally, you can use:

printenv

or

echo $VARIABLE_NAME

Fish Shell

Fish shell uses a different syntax for variables. To display a variable, use:

echo $VARIABLE_NAME

To list all environment variables, use:

set -x

This command shows all exported variables.

How to Display Environment Variables for a Specific User

If you want to see environment variables for a user other than the one logged in, you can switch to that user or check their environment files.

Using su

Switch to the user with:

su - username

Then use printenv or env to see their environment variables.

Checking User Environment Files

User-specific environment variables are often set in files like:

  • ~/.bashrc
  • ~/.bash_profile
  • ~/.profile

You can open these files with a text editor to see what variables are set.

cat ~/.bashrc

or

nano ~/.profile

How to Display Environment Variables in Scripts

When writing shell scripts, you might want to display environment variables for debugging or configuration.

Using echo in Scripts

Inside your script, use:

echo "The PATH is: $PATH"

This prints the value of PATH when the script runs.

Using printenv in Scripts

You can also call printenv inside scripts:

printenv HOME

This helps verify environment variables during script execution.

Tips for Managing Environment Variables

Knowing how to display environment variables is just the start. Here are some tips to manage them effectively:

  • Export variables: Use export VARIABLE=value to make a variable available to child processes.
  • Temporary variables: Set variables for a single session by typing VARIABLE=value without export.
  • Persistent variables: Add export commands to files like ~/.bashrc or /etc/environment to keep variables after reboot.
  • Use env to run commands: You can run a command with a modified environment without changing your current session:
env VARIABLE=value command

Common Environment Variables You Should Know

Here are some important environment variables you might want to display or modify:

VariableDescription
PATHDirectories to search for commands
HOMECurrent user’s home directory
USERCurrent logged-in username
SHELLDefault shell for the user
LANGLanguage and locale settings
PWDCurrent working directory

Checking these variables helps you understand your system’s configuration.

Troubleshooting Environment Variables

Sometimes environment variables don’t behave as expected. Here’s how to troubleshoot:

  • Variable not found? Make sure it’s exported using export VARIABLE.
  • Changes not applied? Reload your shell configuration with source ~/.bashrc or restart the terminal.
  • Variable overridden? Check if other scripts or programs modify the variable.
  • Check variable scope: Local shell variables won’t appear with printenv or env.

Conclusion

Displaying environment variables in Linux is straightforward once you know the right commands. Whether you want to see all variables or just one, tools like printenv, env, and echo make it easy. You can also check variables in different shells, for other users, or inside scripts.

Understanding environment variables helps you customize your Linux experience and troubleshoot problems. Keep these commands handy, and you’ll be able to manage your system environment confidently. Now you can explore your Linux environment variables anytime you want.

FAQs

How do I display all environment variables in Linux?

Use the printenv or env command in the terminal. Both list all current environment variables in your session.

How can I display a specific environment variable?

Type echo $VARIABLE_NAME or printenv VARIABLE_NAME to see the value of a specific variable.

What is the difference between env and printenv?

Both display environment variables, but env can also run commands with a modified environment, while printenv is mainly for printing variables.

How do I display environment variables in a script?

Use echo $VARIABLE_NAME or printenv VARIABLE_NAME inside your script to print variables during execution.

Can I see environment variables for another user?

Yes, switch to that user with su - username and then use printenv or check their environment files like ~/.bashrc.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts