env Command Examples

Environment Variables - Control Your Command Context

About env

The env command displays, sets, or modifies environment variables for command execution. Environment variables control how programs behave, storing configuration like paths, user preferences, and system settings. Understanding env is essential for scripting, debugging, and managing application behavior. It's particularly useful for running commands with modified environments without affecting the current shell session.

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.

Use Case: Debugging environment-dependent issues, documenting system configuration, or understanding what variables are available to scripts and applications.
The output is unsorted. For sorted output, use: 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.

Use Case: Testing programs with different configurations, temporarily changing editor preferences, or running applications with specific settings without permanent changes.

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.

Use Case: Testing how scripts behave without assumptions about the environment, security testing, or debugging environment-related issues by eliminating all variables.
Be careful! With no PATH, even basic commands won't work unless you specify full paths like /bin/ls

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]

Use Case: Configuring applications that read multiple environment variables, testing scripts with various configurations, or setting up complex runtime environments for development.
This is cleaner than exporting variables one by one when you need to run a command with specific settings.

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.

Use Case: Temporarily disabling proxy settings, removing problematic variables for testing, or running commands as if certain environment variables don't exist.

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

Use Case: Quickly checking the value of specific variables, finding variables related to a topic (like all JAVA_ variables), or verifying that expected variables are set.
Use grep -i for case-insensitive search: 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.

Use Case: Testing custom compiled versions of tools, running development versions of utilities, or temporarily prioritizing specific binary locations without modifying your shell's PATH.
To completely replace PATH: 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.

Use Case: Writing portable scripts that work across different Unix/Linux distributions, virtual environments, and user configurations without hardcoding interpreter paths.
Common shebangs: #!/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.

Use Case: Safely parsing environment variables in scripts, handling edge cases where variable values contain newlines, or feeding env output to tools that expect null-terminated input.
This is advanced usage primarily for robust scripting. Most interactive use cases don't need -0.

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

Use Case: Troubleshooting applications, getting detailed execution information, testing behavior under different logging levels, or enabling development features temporarily.
Each application defines its own debug variables. Check documentation for variables like NODE_DEBUG, RUST_BACKTRACE, PYTHONVERBOSE, etc.

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
Practical Use Cases:
  • 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
Debugging Tip: To see exactly what environment a command receives:
env | sort > before.txt
env VAR=value command & env | sort > after.txt
diff before.txt after.txt
Security Note: Environment variables can contain sensitive information (API keys, passwords, tokens). Be careful when displaying them in logs, scripts, or sharing output. Use env | grep -v SECRET to filter sensitive variables.
env vs export: Use 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.