How to Manage Environment Variables Using the env Command in Linux

When you open a Linux terminal and log into a server, the operating system quietly loads dozens of “environment variables” in the background. These hidden variables control critical aspects of your session, such as telling programs exactly where your home directory is located ($HOME), defining what terminal emulator you are using ($TERM), and mapping out where the system should look for executable commands ($PATH). To inspect these hidden variables or temporarily alter them for a single command, you can use the env utility.

How to Print All Environment Variables

To see exactly how your current Linux session is configured, simply type the command with no arguments:

env

Your terminal will immediately output a massive, vertical list of every active environment variable assigned to your user account, formatted as KEY=value. For example, you might see:

USER=admin
LANG=en_US.UTF-8
HOME=/home/admin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Because the output is usually several pages long, it is highly recommended to pipe the output into less or use grep to search for something specific. For example, to check your current language settings, run:

env | grep LANG

Running Commands in a Modified Environment

The true power of the env command is its ability to temporarily alter variables for the execution of a single specific command, without permanently messing up your global bash configuration profile.

For example, if you are a software developer writing a Python script that relies on an API key, you should never hardcode that key into the script. The script should pull the key from the environment. To test your script, you can use env to temporarily inject the API_KEY variable just for that one execution.

env API_KEY="12345ABC" python3 my_script.py

In this scenario, my_script.py will execute perfectly and successfully read the API_KEY. However, the moment the script finishes running, the temporary API_KEY variable will completely vanish. If you run python3 my_script.py again (without the env prefix), the script will fail because the variable no longer exists in the global environment.

Creating a Blank Environment

If you are debugging a complex bash script and suspect that a hidden global variable is causing it to crash, you can use the -i (ignore environment) flag to execute the script in an absolute vacuum. This strips away everything (including the PATH and HOME variables) and forces the script to run with zero external interference.

env -i ./troublesome_script.sh

Get the best tech tips delivered straight to your inbox.

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