Last Updated: February 25, 2016
·
454
· joseraya

Load a bunch of environment variables in OSX

One of the problems I have developing with eclipse on my mac is that there is no way (that I know) to set up a bunch of environment variables globally for a project (you have to set them up at every run configuration, which is a PITA when you need to change one of them).

What I do is to store them in a file called environment at the root of the directory (of course, you should not check it in if it contains sensitive information) like this one:

$ cat environment
DATABASE_URL=mysql://celapi:celapi@192.168.33.11/celapi

And then, this little script will load all the variables so that any application that we open from now on will find them configured:

#!/bin/bash
while IFS='=' read NAME VALUE; do
  echo  "${NAME}: ${VALUE}"
  launchctl setenv $NAME $VALUE
done < environment
source environment

UPDATE Check direnv as an alternative to this as I have found it to be very convenient