# Linux commands: env

> Learn how the Linux env command runs a command with extra environment variables, prints all of them when run alone, and clears the environment with -i.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-10-19 | Topics: [CLI](https://flaviocopes.com/tags/cli/) | Canonical: https://flaviocopes.com/linux-command-env/

The `env` command can be used to pass environment variables without setting them on the outer environment (the current shell).

Suppose you want to run a [Node.js](https://flaviocopes.com/nodejs/) app and set the `USER` variable to it.

You can run

```bash
env USER=flavio node app.js
```

and the `USER` environment variable will be accessible from the Node.js app via the Node `process.env` interface.

You can also run the command clearing all the environment variables already set, using the `-i` option:

```bash
env -i node app.js
```

In this case you will get an error saying `env: node: No such file or directory` because the `node` command is not reachable, as the `PATH` variable used by the shell to look up commands in the common paths is unset.

So you need to pass the full path to the `node` program:

```bash
env -i /usr/local/bin/node app.js
```

Try with a simple `app.js` file with this content:

```js
console.log(process.env.NAME)
console.log(process.env.PATH)
```

You will see the output being

```
undefined
undefined
```

You can pass an env variable:

```bash
env -i NAME=flavio node app.js
```

and the output will be

```
flavio
undefined
```

Removing the `-i` option will make `PATH` available again inside the program:

![Terminal showing env NAME=flavio node app.js output: flavio on first line, PATH variable on second line](https://flaviocopes.com/images/linux-command-env/Screen_Shot_2020-09-10_at_16.55.17.png)

The `env` command can also be used to print out all the environment variables, if ran with no options:

```bash
env
```

it will return a list of the environment variables set, for example:

```
HOME=/Users/flavio
LOGNAME=flavio
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
PWD=/Users/flavio
SHELL=/usr/local/bin/fish
```

You can also make a variable inaccessible inside the program you run, using the `-u` option, for example this code removes the `HOME` variable from the command environment:

```bash
env -u HOME node app.js
```

The `env` command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
