how.wtf

How to pass variables to Makefiles

· Thomas Taylor

Passing variables, exported variables, or .env (dot env) file variables from outside of a Makefile can be completed in a few ways.

Pass variables to Makefile using environment variables

Exported environment variables are referenced by name in the Makefile:

1test:
2	@echo $(TEST)
1export TEST=value
2make test

Output:

1value

Pass variables to Makefile using command line

Similarly, variables may be explicitly passed to make prior to command executions.

1test:
2	@echo $(TEST)
1TEST=value make test

Output:

1value

Pass variables using .env (dot env) files

Makefiles can contain an include directive which reads one or more files before executing.

1ifneq (,$(wildcard ./.env))
2	include .env
3	export
4endif
5
6test:
7	@echo $(TEST)

.env file:

1TEST=dotenv

Output:

1dotenv

#Makefile  

Reply to this post by email ↪