4

This question already has an answer here:

I want to have a file (outside of git) that contains certain environment variables. While a script (which is in git) needs to read them and export to env.

My current implementation works for variables if the value does not contain spaces. Does anyone have an idea on how to fix this?

export $(cat .env | grep -v ^# | xargs -n 1)

My file looks like this:

ALLOWED_HOSTS="127.0.0.1 dev.local localhost boot2docker"

While my environment only contains:

ALLOWED_HOSTS=127.0.0.1

Note: I found a similar question: Set Variable Environment Variables in bash (or other). However those answers don't seem to understand spaces in values either.

marked as duplicate by don_crissti, Jeff Schaller, sam, Kusalananda, Archemar Jul 8 '16 at 7:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

From your one sample line, it looks like your file may be in Bourne-like shell syntax.

So as your shell seems to be of the Bourne-type, you can just source the file after having issued a set -a so each new variable definition be automatically exported (assuming you do want them exported):

set -a
. ./.env
set +a
  • Sometimes there's a very elegant solution. Thank you! – Gert May 26 '15 at 16:12

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