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.
envenv | sort — List all environment variables sorted alphabetically.
env | sortprintenv — Print all environment variables (same as env).
printenvprintenv <VAR> — Print the value of a specific variable.
printenv HOMEecho $<VAR> — Print a variable's value using shell expansion.
echo $PATHenv | grep <pattern> — Search for variables matching a pattern.
env | grep -i proxySet & 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=helloexport <VAR> — Export an already-set shell variable.
MY_VAR=hello && export MY_VARexport -p — List all exported variables with their values.
export -pdeclare -x <VAR>=<value> — Declare and export a variable (Bash).
declare -x API_KEY=abc123Unset & Remove
unset <VAR> — Remove a variable completely.
unset TEMP_VARexport -n <VAR> — Unexport a variable (keep as shell variable only).
export -n MY_VARenv -u <VAR> <command> — Run a command with a variable removed from environment.
env -u http_proxy curl https://example.comInline & Temporary
<VAR>=<value> <command> — Set a variable for a single command only.
NODE_ENV=production node app.jsenv <VAR>=<value> <command> — Run a command with a temporary variable (explicit).
env LANG=C sort data.txtenv -i <command> — Run a command with a completely empty environment.
env -i /bin/bash --norcenv -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 $PATHexport 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 python3Common Patterns
source .env — Load variables from a .env file into the current shell.
source .envexport $(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
- Wikipedia: Environment variable – fundamentals of environment variables and how they are inherited by child processes
- GNU Coreutils: env invocation – the official reference for env, including
-i,-uand-0