Skip to content

Instantly share code, notes, and snippets.

@kristjan
Created October 15, 2012 22:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kristjan/3896117 to your computer and use it in GitHub Desktop.
Save kristjan/3896117 to your computer and use it in GitHub Desktop.
Load a .env file into your current shell. Handy when you need to skirt Foreman.
function loadenv() {
env=${1:-.env}
echo Loading $env
file=`mktemp -t tmp`
if [ -f $env ]; then
cat $env | while read line; do
echo export $line >> $file
done
source $file
else
echo No file $env
fi
}
@jneen
Copy link

jneen commented Oct 18, 2012

You could probably do this without a tmpfile, like so:

if [[ -s "$env" ]]; then
  while read line; do
    eval export "$line"
  done < "$env"
fi

The -s test also makes sure it's readable. I've also really enjoyed direnv for this sort of thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment