Linux Server Admin

Using Dotenv config in Makefile (or anything)

Ever want to use .env file config directly in Makefile? I found it not as easy as it sounds. I found this shell command on the web that read and export a simple .env file (also strip away comments).

Consider the following Makefile, “docker-compose …” can be whatever command you use to compile something or run something.

To export the .env while running the command in “deploy” task , you have to do this:

this key is to use $$ instead of $ to use shell variable, and you need to escape the character # with a backlash \

Shell Tools

If you need to do this a lot, I have invented a little shell tools to use dotenv in anything (really?). Please checkout my git repo dotenv-shell here. Have fun and send me pull requests anytime!

for example, you can simply the above makefile into:

Hope you find this useful!

Share your thoughts