Load .env file into Makefile
Is there an easy way to automatically load a file containing local configurations into Makefile
?
I have a file .env
that contains pairs key=value
on each line:
var1=val1
var2=val2
I am currently importing each variable into mine Makefile
manually like this:
var1=$$(grep '^var1=' .env | cut -d= -f2-)
It seems clunky and error prone when adding additional variables.
I also tried adding an extra target in Makefile
to read each line .env
and pass it to export
, but the values exported to one target are not available to others.
I expect there is a built-in function to do this make
, but I haven't been able to find any documentation for it.
source to share
Assuming your include file consists entirely of valid assignments, then the include
directive is most likely what you want here.
The include directive tells make to pause reading the current makefile and read one or more other makefiles before continuing. A directive is a line in the makefile that looks like this:
include filenames…
filenames can contain shell filename patterns.
source to share