How to View and Modify Environment Variables Using the env Command in Linux

When you are writing complex bash scripts or deploying software on a Linux server, the environment variables dictating how applications behave are largely invisible. Variables define critical system pathways, such as where the operating system should look for executable binaries ($PATH) or what the default text editor is ($EDITOR). If a script is failing to execute because it cannot locate a dependency, checking the current environment variables should be your first troubleshooting step. You can instantly list and temporarily modify these variables using the Linux env command.

How to Display All Environment Variables

The simplest way to use the env command is to run it with absolutely no arguments. Open your terminal and type:

env

This command will dump a massive, unformatted list of every single environment variable currently active in your user session, formatted as KEY=value. Because the output is usually several pages long, it is highly recommended to pipe the output into the less command so you can scroll through it comfortably.

env | less

If you are looking for a specific variable, such as the current PATH, you can pipe the output into grep to instantly filter the results.

env | grep PATH

How to Modify a Variable Temporarily

The true power of the env command is not just displaying variables, but modifying them on the fly for a single command without permanently altering your system configuration.

For example, if your default system language (LANG) is set to English, but you want to run a specific Python script and force it to execute in French to test the localization, you can use env to temporarily override the variable.

env LANG=fr_FR.UTF-8 python3 test_script.py

In this example, the Python script will launch believing the entire system is in French. However, the moment the script finishes executing, the LANG variable instantly reverts back to English for your main terminal session.

How to Clear the Environment

If you are debugging a highly sensitive security script and you want to guarantee that it is not inheriting any rogue variables from your user profile, you can use the -i (ignore environment) flag. This forces the command to run in a completely empty, sterile environment.

env -i ./my_secure_script.sh

The script will execute with absolutely no environment variables present, ensuring maximum predictability.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.