env Command

The env command displays environment variables or runs programs in a modified environment. It's useful for viewing current environment settings or executing commands with specific environment variables.

Syntax

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

Description

The env command can display all environment variables or run a command in a modified environment. It's commonly used in shell scripts and for debugging environment-related issues.

Key features:

  • Display all environment variables
  • Run commands with modified environment
  • Clear environment before running commands
  • Set temporary environment variables
  • Useful for shell scripting and debugging

Common Options

Option Description
-i Start with empty environment
-u NAME Remove variable NAME from environment
-0 End output lines with NUL character
--help Display help information
--version Show version information

Examples

Display all environment variables

env

Shows all current environment variables and their values

Run command with environment variable

env PATH=/usr/local/bin:$PATH ls

Runs ls with modified PATH variable

Set multiple environment variables

env VAR1=value1 VAR2=value2 command

Runs command with multiple environment variables set

Start with clean environment

env -i bash

Starts bash with empty environment

Remove environment variable

env -u HOME bash

Starts bash without the HOME variable

Display specific variable

env | grep PATH

Shows only PATH-related environment variables

Run script with custom environment

env DEBUG=1 VERBOSE=true ./myscript.sh

Runs script with debug and verbose flags set

Clean environment with minimal variables

env -i PATH=/bin:/usr/bin HOME=/tmp bash

Starts bash with only PATH and HOME set

Check if variable is set

env | grep "^DISPLAY="

Checks if DISPLAY variable is set

Temporary environment for testing

env LANG=C LC_ALL=C date

Runs date command with C locale

See also