Shell – set environment variables from stdout

environment-variablespipeshell

How can you do the equivalent of piping stdout to 'export'?

For background, I have a non-shell script that generates environment variables like so:

DATABASE_URL='someurl'
MAIL_KEY='key'
REDIS_URL='redis connection string'

I would like to take that output and execute it, something like:
generate-env | xargs export

Unfortunately, export isn't a binary, and neither is eval. I would prefer not to have to write my output to a file and run it with source. Is this even possible?

Best Answer

You can use eval:

$ set -a
$ eval "$(command_that_generate_output)"
$ set +a
$ sh -c 'printf "%s\n" "$DATABASE_URL"'
someurl
Related Question