Reputation: 2273
I'm trying to export environment variables so that they can be accessed in the parent shell MakeFile:
export-vars:
# extract vars from text file - remove comments - export output
export $$(cat env.txt | grep -v ^# | xargs)
Then when I run:
$ make export-vars
$ echo $SOME_VAR_EXPORTED_FROM_MAKEFILE
I am expecting the vars in the text to be set as environment vars.
Any help how to do this within a target/rule?
Upvotes: 3
Views: 12093
Reputation: 101081
You can't do this with a makefile OR with a separate shell script. When you run make, or run a shell script, it creates a new process. Any changes made to the environment of that process will all be tossed away when that process exits. It's not possible (in a UNIX-like system) for a process to modify the environment of its parent process.
The only thing you can do is either write a shell function, which is invoked within the shell process you're running (which is displaying your prompt, etc.) or have a script that you source, not run: that will cause the script to run in the same shell process rather than creating a new one.
So you could write a shell function in your ~/.bashrc
or similar like this:
setvars () { export $(grep -v ^# env.txt | xargs); }
Or you could write a separate shell script, but you can't run it you have to source it (the .
command sources a script rather than running it):
. set-my-vars.sh
Upvotes: 8