# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/shell-system/env/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## View Variables

`env` — List all environment variables.

```bash
env
```

`env | sort` — List all environment variables sorted alphabetically.

```bash
env | sort
```

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

```bash
printenv
```

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

```bash
printenv HOME
```

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

```bash
echo $PATH
```

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

```bash
env | grep -i proxy
```

## Set & Export

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

```bash
export EDITOR=vim
```

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

```bash
MY_VAR=hello
```

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

```bash
MY_VAR=hello && export MY_VAR
```

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

```bash
export -p
```

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

```bash
declare -x API_KEY=abc123
```

## Unset & Remove

`unset <VAR>` — Remove a variable completely.

```bash
unset TEMP_VAR
```

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

```bash
export -n MY_VAR
```

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

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

## Inline & Temporary

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

```bash
NODE_ENV=production node app.js
```

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

```bash
env LANG=C sort data.txt
```

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

```bash
env -i /bin/bash --norc
```

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

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

## PATH Management

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

```bash
echo $PATH
```

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

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

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

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

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

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

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

```bash
which python3
```

## Common Patterns

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

```bash
source .env
```

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

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

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

```bash
echo ${PORT:-8080}
```

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

```bash
echo ${DATABASE_URL:?DATABASE_URL must be set}
```

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

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

<!-- PROSE:outro -->
## 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](https://en.wikipedia.org/wiki/Environment_variable) – fundamentals of environment variables and how they are inherited by child processes
- [GNU Coreutils: env invocation](https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html) – the official reference for env, including `-i`, `-u` and `-0`
<!-- PROSE:outro:end -->

## Related Commands

- [bash](https://www.jpkc.com/db/en/cheatsheets/shell-system/bash/) – the shell where variables are set, exported and expanded
- [id](https://www.jpkc.com/db/en/cheatsheets/shell-system/id/) – shows the user and group IDs that help shape the environment's behaviour
- [sudo](https://www.jpkc.com/db/en/cheatsheets/shell-system/sudo/) – run commands with different privileges, including `-E` to preserve the environment

