env Command Cheat Sheet
env allows you to run a command in a modified environment. It is typically used to set or unset environment variables temporarily for a single command execution, or to list current variables.
Synopsis
env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]
Basic Usage
List All Environment Variables
Running env without arguments prints all exported environment variables.
env
Run Command with Specific Variable
Set VAR to value only for the duration of command.
env EDITOR=nano crontab -e
Modifying the Environment
Set Multiple Variables
env USER=test PATH=/bin:/usr/bin script.sh
Unset a Variable (-u)
Remove a variable from the environment for the command.
env -u EDITOR crontab -e
Clear Entire Environment (-i)
Run with an empty environment. Useful for testing scripts in isolation to ensure they don't rely on user-specific settings.
env -i /bin/bash
Or combining with specific variables:
# Start with empty env, but add PATH and TERM
env -i PATH=/bin:/usr/bin TERM=xterm /bin/bash
The Shebang Usage
env is standard for shebang lines in scripts because absolute paths to interpreters vary (e.g., python might be in /usr/bin or /usr/local/bin).
Python Script
#!/usr/bin/env python3
python3 in the user's $PATH and run it".
Node.js Script
#!/usr/bin/env node
Bash Script
#!/usr/bin/env bash
Debugging
Print Specific Variable
Use printenv generally, but env with grep works too.
env | grep PATH
Trace Execution (-v)
Verbose mode prints processing steps (GNU coreutils 8.30+).
env -v ls
Options Cheat Sheet
| Option | Description |
|---|---|
-i, --ignore-environment |
Start with an empty environment |
-u, --unset=NAME |
Remove variable from the environment |
-0, --null |
End each output line with NUL, not newline |
-C, --chdir=DIR |
Change directory before running command |
-S, --split-string=S |
Process and split S into arguments (for shebangs) |
Advanced: Breaking Arguments in Shebangs (-S)
Standard shebangs only accept one argument. To pass multiple flags (e.g., awk -f), use -S.
#!/usr/bin/env -S awk -f
Comparison: env vs printenv vs export
| Command | Purpose | Scope |
|---|---|---|
env |
Run command with modified env | Process execution |
printenv |
Display env variables | Display only |
export |
Set variable in current shell | Shell session |
set |
List shell variables + functions | Shell internal |
Practical Examples
Testing Compilation in Clean Environment
Ensure your build doesn't depend on accidental environment variables.
env -i PATH=$PATH make
Changing Directory Before Running (-C)
Similar to (cd /tmp && ls).
env -C /tmp ls
Force English Locale
Run a command with default English output, ignoring user's language settings.
env LC_ALL=C date
# Output: Sat Feb 14 12:00:00 UTC 2026
# Instead of localized format
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 126 | Command found but cannot be invoked |
| 127 | Command not found |
Notes
envis external (/usr/bin/env), unlikeexportwhich is a shell builtin.- Altering
PATHwithenvis a common security risk if untrusted directories are added first.