I'm trying to include some env vars into a Makefile. The env file looks like:


Note there's no leading export to each env var. If I add the leading export and just include the env file in the Makefile, everything works as it should. But I need to keep the env vars sans leading export. That prevents me from just using include envfile in the Makefile.

I've also tried doing something like this:

sed '/^#/!s/^/export /' envfile > $(BUILDDIR)/env
include $(BUILDDIR)/env

But doing that cause make to throw an error because the env file isn't there for including.

  • 1
    Welcome to Unix&Linux. Please state the question you have clearly. What is it exactly that you're trying to accomplish?
    – darnir
    Commented Oct 10, 2015 at 16:40
  • 1
    I'm trying to include a file that contains environment variable pairs into a Makefile so that they are available to the environment in which make is running. Commented Oct 10, 2015 at 17:06

8 Answers 8


I had a similar problem and landed on this thread. Another possible solution is to just do the make export command without any arguments:

include .env

This will export all make variables as environment variables. This behavior may or may not match all use cases, but it worked for me and didn't involve any shell scripting wizardry.

  • 5
    For better backwards compatibility, I'd recommend setting the .EXPORT_ALL_VARIABLES: special target instead of using the export directive, as described here in the manual: gnu.org/software/make/manual/html_node/…
    – jlucktay
    Commented Jul 26, 2021 at 5:27
  • 1
    Worth to hint that if .env doesn't exist, GNU Make will raise .env: No such file or directory
    – artu-hnrq
    Commented Sep 1, 2022 at 15:28
  • Doing -include .env will prevent the error, which may or may not be the desired result
    – Marvin
    Commented Feb 23, 2023 at 19:30
  • This creates infinite recursion in make >=4.4 as in this exmample: github.com/photoprism/photoprism/discussions/3790 How do you fix it?
    – tback
    Commented Oct 4, 2023 at 18:47

If you are using gnu make, what should work is to include the envfile file, then export the list of vars got from the same file:

include envfile
export $(shell sed 's/=.*//' envfile)

  • 13
    Thanks for this answer, it's brilliant! Although my version even works without the export $(shell... line. I just need the include line. Commented Jan 15, 2017 at 12:45
  • 6
    It's worth noting that if you have any $ in the env file make won't like them and you'll have to escape them. PASS=Pass123$ will just show up as Pass123. This answer explains the caveats pretty well. stackoverflow.com/a/44637188/3384609
    – ClintM
    Commented May 17, 2020 at 15:13

an example of how you can use .env values in Makefile

include .env

    migrate create -ext sql -dir mysql/migrations -seq int_schema

    migrate -path mysql/migrations -database "mysql://${DB_USER_NAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}" -verbose up 1

    migrate -path mysql/migrations -database "mysql://${DB_USER_NAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}" -verbose down 1

To complete previous anwswer from meuh, if you would like env file to be dynamic, you could define a setup function.

  • create env/local-dev.env,env/integ.env file with some content
  • define a method to load this
    define setup_env
        $(eval ENV_FILE := env/$(1).env)
        @echo " - setup env $(ENV_FILE)"
        $(eval include env/$(1).env)
        $(eval export sed 's/=.*//' env/$(1).env)

Then use it

    localDevEnv: ## load env/local-dev.env environment
        $(call setup_env,local-dev)
    integEnv: ## load env/integ.env environment
        $(call setup_env,integ)
  • 3
    Works like a charm!
    – Piotr
    Commented Jul 2, 2020 at 12:36

Preserves pre existing ENV vars

export $(shell [ ! -n "$(ENVFILE)" ] || cat $(ENVFILE) | grep -v \
    --perl-regexp '^('$$(env | sed 's/=.*//'g | tr '\n' '|')')\=')

    echo $$FOO

To run

make ENVFILE=envfile test # bar

export FOO=foo
make ENVFILE=envfile test # foo

Here is an elegant solution that does not import Make vars, but does set any environment exports for shell subprocesses. Personally I prefer to keep my profiles dynamic scripts.

export BASH_ENV=tools/sh/env.sh

There are some gotchas. Unfortunately ENV does not work, I think it should (but that's an issue with Bash, not Make). Also it really requires another line, because Make's default shell interpreter is /bin/sh.

BASH_ENV := tools/sh/env.sh
SHELL := /usr/bin/bash

On the up-side this also allows to include shell functions in Makefile scripts, but only for the recipe scripts. I don't know how other shells besides Bash fare. And there is a bit more going on. I put some testfile up as GIST.

There are plenty of complicated shell scripts imaginable. I like this Q/A best for the simple answers.


if your filename is .makerc then you can include it to Makefile as simple as this:

include .makerc

I prefer to add - sign before include, to suppress any warnings.

  • 2
    I can't verify this on make 4.2.1. without export after the include.
    – JohannesM
    Commented Jan 25, 2021 at 18:20

If you have bash, you can use allexport option before loading a .env file.

# sample usage:
#   $ make from-env RECIPE=another-recipe
    bash -c 'set -o allexport; source .env; set +o allexport; make "$$RECIPE"'


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .