1. Display All Environment Variables
$ env
Shows all current environment variables and their values. This displays the complete environment that will be inherited by child processes.
Typical output includes: PATH, HOME, USER, SHELL, LANG, and dozens of other variables that control system and application behavior.
env | sort
2. Run Command with Modified Environment Variable
$ env EDITOR=nano git commit
Runs a command with a temporarily modified environment variable. In this example, git commit will use nano as the editor instead of whatever EDITOR is normally set to.
Key point: The change only affects this command execution; your shell's EDITOR remains unchanged.
3. Run Command with Clean Environment
$ env -i bash --noprofile --norc
The -i flag starts with an empty environment, removing all inherited variables. This creates a truly clean slate for command execution.
Result: A minimal shell with no PATH, HOME, USER, or other inherited variables. Useful for testing and debugging.
4. Set Multiple Variables for Command
$ env VAR1=value1 VAR2=value2 VAR3=value3 ./myscript.sh
Sets multiple environment variables simultaneously for a single command execution. All variables are set before the command runs.
Syntax: env VAR=value [VAR=value ...] command [args]
5. Remove Specific Variable from Environment
$ env -u HTTP_PROXY curl http://example.com
The -u flag unsets (removes) a specific variable for the command execution. In this example, curl runs without the HTTP_PROXY variable, bypassing any proxy settings.
Multiple unsets: You can use -u multiple times to remove several variables.
6. Search for Specific Environment Variable
$ env | grep PATH
Filters environment variables to show only those matching a pattern. This is useful when you know the variable name or part of it.
Common searches: PATH, HOME, USER, LANG, SHELL, PWD
env | grep -i path
7. Run Command with Modified PATH
$ env PATH=/custom/bin:$PATH mycommand
Prepends a custom directory to PATH for a single command execution. The command will search /custom/bin first, then fall back to the normal PATH.
$PATH expansion: The existing PATH is preserved and extended, not replaced.
env PATH=/only/this/dir mycommand
8. Use env as Shebang Line
#!/usr/bin/env python3
When used in a script's shebang line, env finds the interpreter in the user's PATH. This makes scripts more portable across different systems where interpreters may be in different locations.
Why this matters: python3 might be in /usr/bin, /usr/local/bin, or ~/.local/bin depending on the system.
#!/usr/bin/env bash, #!/usr/bin/env node, #!/usr/bin/env ruby
9. Run Command with Null-Terminated Output
$ env -0 | xargs -0 -I {} echo "{}"
The -0 flag outputs variables separated by null characters instead of newlines. This is crucial for handling variables that contain newlines or special characters.
Why null-terminated? Newlines can exist in variable values, but null characters cannot, making parsing safer.
10. Debug Script with Verbose Environment
$ env DEBUG=1 VERBOSE=true LOG_LEVEL=debug ./application
Many applications check environment variables for debugging flags. Running with debug variables enabled provides detailed output without modifying the application code or configuration files.
Common debug variables: DEBUG, VERBOSE, LOG_LEVEL, TRACE, DRY_RUN
Common Environment Variables
| Variable | Purpose |
|---|---|
| PATH | Directories to search for executable files |
| HOME | User's home directory path |
| USER | Current username |
| SHELL | Path to user's default shell |
| PWD | Current working directory |
| LANG | System language and locale |
| EDITOR | Default text editor for programs |
| TERM | Terminal type for display capabilities |
| DISPLAY | X11 display server location |
| LD_LIBRARY_PATH | Additional library search paths |
Quick Reference: env Options
| Option | Description |
|---|---|
-i |
Start with empty environment (ignore inherited variables) |
-u VAR |
Remove variable VAR from the environment |
-0 |
Use null character as separator (not newline) |
-C DIR |
Change to directory DIR before executing command |
-S STRING |
Process and split STRING into separate arguments |
--help |
Display help information |
--version |
Display version information |
- Run Python with UTF-8 encoding:
env PYTHONIOENCODING=utf-8 python3 script.py - Disable proxy for one command:
env -u http_proxy -u https_proxy wget http://example.com - Test with different timezone:
env TZ=America/New_York date - Run Node.js with more memory:
env NODE_OPTIONS="--max-old-space-size=4096" node app.js - Enable Rust backtrace:
env RUST_BACKTRACE=1 ./my_rust_program
env | sort > before.txtenv VAR=value command & env | sort > after.txtdiff before.txt after.txt
env | grep -v SECRET to filter sensitive variables.
export when you want to set a variable for your current shell and all child processes. Use env when you want to set a variable for just one command without affecting your shell.