env — Manage Environment Variables in the Shell

View, set, export and unset environment variables in the shell – plus PATH management, .env files and the portable env shebang.

Environment variables control how programs behave: from the search PATH and the locale LANG to API keys and proxy settings. env is the tool you reach for to inspect those variables, set one cleanly for a single invocation, or reset the environment entirely – and together with the shell builtins export and unset you manage the whole lifecycle. This guide walks you through the commands you actually use day to day, from a quick lookup to a clean, reproducible environment.

View Variables

env — List all environment variables.

env

env | sort — List all environment variables sorted alphabetically.

env | sort

printenv — Print all environment variables (same as env).

printenv

printenv <VAR> — Print the value of a specific variable.

printenv HOME

echo $<VAR> — Print a variable's value using shell expansion.

echo $PATH

env | grep <pattern> — Search for variables matching a pattern.

env | grep -i proxy

Set & Export

export <VAR>=<value> — Set and export a variable (available to child processes).

export EDITOR=vim

<VAR>=<value> — Set a shell variable (NOT exported to child processes).

MY_VAR=hello

export <VAR> — Export an already-set shell variable.

MY_VAR=hello && export MY_VAR

export -p — List all exported variables with their values.

export -p

declare -x <VAR>=<value> — Declare and export a variable (Bash).

declare -x API_KEY=abc123

Unset & Remove

unset <VAR> — Remove a variable completely.

unset TEMP_VAR

export -n <VAR> — Unexport a variable (keep as shell variable only).

export -n MY_VAR

env -u <VAR> <command> — Run a command with a variable removed from environment.

env -u http_proxy curl https://example.com

Inline & Temporary

<VAR>=<value> <command> — Set a variable for a single command only.

NODE_ENV=production node app.js

env <VAR>=<value> <command> — Run a command with a temporary variable (explicit).

env LANG=C sort data.txt

env -i <command> — Run a command with a completely empty environment.

env -i /bin/bash --norc

env -i HOME=$HOME PATH=$PATH <command> — Run with clean environment but keep essentials.

env -i HOME=$HOME PATH=$PATH bash -c 'env'

PATH Management

echo $PATH — Show the current PATH (colon-separated directories).

echo $PATH

export PATH="<dir>:$PATH" — Prepend a directory to PATH (searched first).

export PATH="$HOME/.local/bin:$PATH"

export PATH="$PATH:<dir>" — Append a directory to PATH (searched last).

export PATH="$PATH:/opt/myapp/bin"

echo $PATH | tr ':' '\n' — Show PATH entries one per line.

echo $PATH | tr ':' '\n'

which <command> — Show which PATH entry a command resolves to.

which python3

Common Patterns

source .env — Load variables from a .env file into the current shell.

source .env

export $(grep -v '^#' .env | xargs) — Export all non-comment lines from a .env file.

export $(grep -v '^#' .env | xargs)

${VAR:-default} — Use a default value if variable is unset or empty.

echo ${PORT:-8080}

${VAR:?error message} — Error and exit if variable is unset or empty.

echo ${DATABASE_URL:?DATABASE_URL must be set}

env -0 | sort -z | tr '\0' '\n' — Safely list variables (handles values with newlines).

env -0 | sort -z | tr '\0' '\n'

Conclusion

env is more than just "list all variables": with env VAR=value command you set a variable cleanly for exactly one invocation without touching your shell environment, and with env -i you launch a program in a completely empty environment – ideal for reproducible builds and for debugging variable-related issues. env is also ubiquitous in the shebang #!/usr/bin/env python3: instead of hard-coding a fixed interpreter path, the program is looked up via PATH, which keeps scripts portable across systems and virtual environments. A security note: secrets such as API keys or passwords are not truly private as environment variables – depending on the system they show up in /proc/<pid>/environ and are inherited by child processes. For real secrets, dedicated secret managers or files with restrictive permissions are the better choice. And be careful with the popular export $(grep -v '^#' .env | xargs): it breaks on values containing spaces or quotes – for complex .env files, prefer set -a; source .env; set +a.

Further Reading

  • bash – the shell where variables are set, exported and expanded
  • id – shows the user and group IDs that help shape the environment's behaviour
  • sudo – run commands with different privileges, including -E to preserve the environment