Question: I have one of those `.env` files with several variables set, like:

```
VAR1=111
VAR2=222

```

I want to run a script (without changing it) that makes use of those variables. 
I know I can `source` that file in the shell and export the variables from the file, like:

```
set -a
source myenvs.env   # this is my .env file
set +a
./myscript.sh  # this will use the variables

```

The problem with that is it loads all those variables in my shell, which I don't want (shell pollution.) 

What I'm looking for is the equivalent of using `env VAR1=111 VAR2=222 ./myscript.sh` but getting those variables from the .env file instead.

Answer: 
Do that in a subshell and main shell's environment won't be altered. E.g:

```
(
  set -a
  source myenvs.env   # this is my .env file
  exec ./myscript.sh  # avoid unnecessary fork()
)

```

> 
> The truth is I need to keep that script flexible to run with different combinations of settings. Imagine I have several .env files like development.env, stagin.env, repro-bug-123.env, etc.
> 
> 
> 

A function would be more useful in that case.

```
with_env() (
  set -a
  source "$1"
  shift
  exec "$@"
)

with_env myenvs.env ./myscript.sh

```
Declare vars that you don't want to change as read only

```
declare -r VAR3 VAR4

```

and source the rest

```
. ./myenvs.env

```