env Cheatsheet
Quick reference for listing, clearing, and overriding environment variables with env in Linux
The `env` command prints the current environment or runs another command with a modified environment. This cheatsheet covers listing variables, starting with a clean environment, overriding values temporarily, and common troubleshooting patterns.
Basic Syntax
Core env command forms.
| Command | Description |
|---|---|
env | Print the current environment |
env --help | Show available options |
env --version | Show the installed env version |
env -0 | Print variables separated with NUL bytes |
Inspect Environment Variables
Use env with filters to inspect specific variables.
| Command | Description |
|---|---|
| `env | sort` |
| `env | grep ‘^PATH='` |
| `env | grep ‘^HOME='` |
| `env | grep ‘^LANG='` |
Run Commands with Temporary Variables
Set variables for one command without changing the current shell session.
| Command | Description |
|---|---|
VAR=value env command | Run one command with a temporary variable |
VAR1=dev VAR2=1 env command | Set multiple temporary variables |
env PATH=/custom/bin:$PATH command | Override PATH for one command |
env LANG=C command | Run a command with the C locale |
env HOME=/tmp bash | Start a shell with a temporary home directory |
Clean or Remove Variables
Start with a minimal environment or remove selected variables.
| Command | Description |
|---|---|
env -i command | Run a command with an empty environment |
env -i PATH=/usr/bin:/bin bash --noprofile --norc | Start a mostly clean shell with a minimal PATH |
env -u VAR command | Run a command without one variable |
env -u http_proxy command | Remove a proxy variable for one command |
env -i VAR=value command | Run a command with only the variables you set explicitly |
Common Variables
These variables are often inspected or overridden with env.
| Variable | Description |
|---|---|
PATH | Directories searched for commands |
HOME | Current user’s home directory |
USER | Current user name |
SHELL | Default login shell |
LANG | Locale and language setting |
TZ | System timezone (e.g. America/New_York) |
EDITOR | Default text editor |
TERM | Terminal type (e.g. xterm-256color) |
TMPDIR | Directory for temporary files |
PWD | Current working directory |
Troubleshooting
Quick checks for common env issues.
| Issue | Check |
|---|---|
| A temporary variable does not persist | env VAR=value command affects only that command and its children |
A command is not found after env -i | Add a minimal PATH, such as /usr/bin:/bin |
| Output is hard to parse safely | Use env -0 with tools that support NUL-delimited input |
| A variable is still visible in the shell | env does not modify the parent shell; use export or unset the variable in the shell itself |
| A locale-sensitive command behaves differently | Check whether LANG or related locale variables were overridden |
Related Guides
Use these guides for broader environment-variable workflows.
| Guide | Description |
|---|---|
| How to Set and List Environment Variables in Linux | Full guide to listing and setting environment variables |
| export Command in Linux | Export shell variables to child processes |
| Bashrc vs Bash Profile | Understand shell startup files |
| Bash cheatsheet | Quick reference for Bash syntax and variables |