LinuxCommandLibrary

env

run programs with modified environment variables

TLDR

Show all environment variables

$ env
copy
Run command with environment
$ env VAR=value [command]
copy
Clear environment
$ env -i [command]
copy
Unset variable
$ env -u [VAR] [command]
copy

SYNOPSIS

env [options] [VAR=value]... [command]

DESCRIPTION

env runs a program in a modified environment. It can display current environment variables, set new ones, unset existing ones, or run commands with a clean environment.
The command is useful for setting variables temporarily or debugging environment issues.

PARAMETERS

-i, --ignore-environment

Start with empty environment
-u var, --unset=var
Remove variable from environment
-0, --null
End lines with NUL, not newline
-C dir, --chdir=dir
Change directory before running command
-S string
Process and split string into arguments

WORKFLOW

$ # Display all environment variables
env

# Set variable for one command
env DEBUG=1 ./program

# Multiple variables
env VAR1=value1 VAR2=value2 command

# Clear environment
env -i command

# Clean environment with specific variables
env -i PATH=/usr/bin HOME=/home/user command

# Unset variable
env -u DISPLAY command

# Change directory first
env -C /tmp ls

# In shebang for portability
#!/usr/bin/env python3
copy

COMMON USES

Shebang lines:

$ #!/usr/bin/env bash
#!/usr/bin/env python3
#!/usr/bin/env node
copy
Temporary variables:
$ env NODE_ENV=production npm start
env DATABASE_URL=postgres://... rails console
copy
Clean environment:
$ env -i PATH="$PATH" command
copy
Debugging:
$ env | grep PATH
env | sort
copy

VARIABLE FORMAT

Variables shown as:

$ NAME=value
PATH=/usr/bin:/bin
HOME=/home/user
copy

COMPARISON WITH EXPORT

env

Runs command with modified environment
export
Sets variables for current shell and children
$ # env (one command only)
env VAR=value command

# export (persistent)
export VAR=value
command  # VAR is available
copy

CAVEATS

Variables set with env don't persist after command exits. Shell built-ins may not be accessible with env. PATH must be set for clean environment. Quotes needed for values with spaces. Different from the export shell built-in.

HISTORY

env has been part of Unix since the early days, included in POSIX standards for environment manipulation.

SEE ALSO

export(1), printenv(1), set(1)

> TERMINAL_GEAR

Curated for the Linux community

Raspberry Pi 5 8GB

Raspberry Pi 5 8GB

$134.50
KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

KIWITATA 2X Classic SNES USB Controller for PC Gamings, Retro SNES Wired USB Game Emulator Controller for Windows XP 7 8 11 Mac Raspberry Pi

$16.99
nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

$129.95
Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community

Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

$1,589.00
Raspberry Pi 5 8GB

Raspberry Pi 5 8GB

$134.50
GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

$739.99