Home > Software engineering >  How can you export all the variables in a file you use source on?
How can you export all the variables in a file you use source on?

Time:07-29

I have 2 files at the moment that is 1 too many. I have a file named ../environment that is included into my service descriptions for systemd. Then I have a postactivate file that virtualenvwrapper will source for me after I activate a VENV.

What I want is to source the ../environment file from inside the postactivate file BUT systemd environment files can't include the word export and the sourced file needs export to do what needs to be done. Is there any option I can use to export all variables defined in ../environment so that I don't have to maintain two equivalent sets of variables in two different forms?

Here is an example:

../evironment

DB_NAME=foo

$VIRTUAL_ENV/bin/postactivate

export DB_NAME=foo

And I want: $VIRTUAL_ENV/bin/postactivate

set -export-all-variables
source ../environment
unset -export-all-variables

Ignoring for a moment the pathing issues, is there some option I haven't found in bash to do what I'm asking?

Also vscode doesn't want the export statements either so I'd really like to collapse this down to 1 file

I guess my other option would be something like eval $(sed s//export / ../environment)

CodePudding user response:

You can use process substitution:

source <(sed 's/^/export /' ../environment)
  •  Tags:  
  • bash
  • Related