How to pass variables to Makefiles
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 testOutput:
1valuePass 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 testOutput:
1valuePass 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=dotenvOutput:
1dotenv