foreman can read .env files and set environment variables from the contents, and then run a program

e.g. foreman run -e vars.env myprogram

...but it does a lot of other things (and is primarily concerned with starting things using its Procfile format).

Is there a simpler (Linux/Unix) tool that's just focussed on reading .env files and executing a command with the new environment?

Example environment file (from http://ddollar.github.io/foreman/#ENVIRONMENT ):

  • 1
    Can you post an example .env file? I suspect bash .env or sh .env may work? Sep 20, 2013 at 1:28
  • @IVlint67 I've improved the question a little.
    – wodow
    Sep 20, 2013 at 13:26

4 Answers 4


You can source the environment file in the active shell and run the program:

sh -ac ' . ./.env; /usr/local/bin/someprogram'

The -a switch exports all variables, so that they are available to the program.

  • 3
    bash -ac 'source .env && ./program'
    – fiatjaf
    Jan 2, 2019 at 14:13
  • 1
    @fiatjaf Why would you use bash in this case if the POSIX shell does the job and you need no feature that actually requires bash? Furthermore, bash is not available by default on all systems (e.g. FreeBSD).
    – Marco
    Jan 2, 2019 at 19:46
  • 1
    Oh, right, makes sense, I think your way is better, then. I was just providing the Bash alternative because I felt more comfortable writing it.
    – fiatjaf
    Jan 3, 2019 at 21:03

Another alternative is envdir:

envdir runs another program with environment modified according to files in a specified directory.


I tried source .env and it worked like a charm. Unfortunately, none of the other solutions posted here worked for me.

  • This works only for valid bash (strings like export KEY=VALUE, not KEY=VALUE)
    – LennyLip
    Jul 27, 2021 at 4:01

This works:

env $(cat .env | tr "\\n" " ") myprogram

but obviously doesn't check the format of the .env file for correctness, which a utility program would do.

  • 1
    1) The cat is not necessary, just write tr "\\n" " " < .env 2) This breaks if multi-line assignments are used.
    – Marco
    Sep 20, 2013 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.