« Back to Index

Make: environment variables and passing values to make target

View original Gist on GitHub

Tags: #make #makefile #args #env #vars

Makefile environment variables and passing values to make target.markdown

In the following example the value of beep will only be used when there is no existing TF_LOG data anywhere.

TF_LOG ?= "beep"
run:
	echo $(TF_LOG)
	env | grep -i tf_log

Example run:

$ export TF_LOG=test # set TF_LOG in the parent shell
                     # make will copy this when spinning up a new shell (when running a makefile target) 

$ make run
echo test
test
env | grep -i tf_log
TF_LOG=test # value of 'beep' was overridden by the parent shell environment
                                                                                                                                                                                           $ make run -e TF_LOG=trace
echo trace
trace
env | grep -i tf_log
MAKEFLAGS=e -- TF_LOG=trace
TF_LOG=trace # value of 'beep' AND parent shell environment was overridden by make's -e flag (which sets env var within the child shell process it spins up)
                                                                                                                                                                                          
$ make run TF_LOG=trace
echo trace
trace
env | grep -i tf_log
MAKEFLAGS=TF_LOG=trace
TF_LOG=trace # value of 'beep' AND parent shell environment was overridden by passing TF_LOG=trace as a makefile 'argument'.
                                                                                                                                                                                           $ TF_LOG=trace make run
echo trace
trace
env | grep -i tf_log
TF_LOG=trace # value of 'beep' AND parent shell environment was overridden by passing TF_LOG=trace as command scoped environment var.
                                                                                                                                                                                           $ unset TF_LOG  # to demonstrate when 'beep' will be used
                # as TF_LOG doesn't exist in current shell (so Makefile can't copy it)
                # nor do we pass in TF_LOG via -e flag or pre/post make target itself

$ make run
echo "beep"
beep
env | grep -i tf_log
make: *** [run] Error 1