Thanks to OP and other posters - various ideas useful in different cases.<p>The xargs idea made me think of using bash as the parser :<p><pre><code> bash -c "exec -c bash -c 'source $CONFIG/main.bash; env'"</code></pre>
This test .bash file contains multiple source-s of other .bash files, which contain a mix of comments, functions, set and env vars - just the env vars are exported by env.
This seems useful e.g. for collating & summarising an environment for docker run -e.<p>This outputs the env vars to stdout; for the OP's purpose, the output could be sourced :<p><pre><code> envFile=$(mktemp /tmp/env.XXXXXX);
bash -c "exec -c bash -c 'source $CONFIG/main.bash; env'" > $envFile;
env $(cat $envFile) sh -c 'echo $API_HOST'
</code></pre>
# For Bourne shell, use env -i in place of exec -c :<p>sh -c "env -i sh -c '. $CONFIG/main.sh; env'" > $envFile