How to set the environment variable of a child process in a Makefile

I want to change this Makefile:

SHELL := /bin/bash PATH := node_modules/.bin:$(PATH) boot: @supervisor \ --harmony \ --watch etc,lib \ --extensions js,json \ --no-restart-on error \ lib test: NODE_ENV=test mocha \ --harmony \ --reporter spec \ test clean: @rm -rf node_modules .PHONY: test clean 

at

 SHELL := /bin/bash PATH := node_modules/.bin:$(PATH) boot: @supervisor \ --harmony \ --watch etc,lib \ --extensions js,json \ --no-restart-on error \ lib test: NODE_ENV=test test: mocha \ --harmony \ --reporter spec \ test clean: @rm -rf node_modules .PHONY: test clean 

Unfortunately, the second one does not work (the node process still works with the default NODE_ENV .

What did I miss?

+97
shell environment-variables makefile target
May 24 '14 at 8:52
source share
3 answers

Make variables are not exported to the make invokes ... process environment by default. However, you can use make export to force them to do this. Change:

 test: NODE_ENV = test 

:

 test: export NODE_ENV = test 

(assuming you have a fairly modern version of GNU make).

+127
May 24 '14 at
source share

As MadScientist noted , you can export individual variables with:

 export MY_VAR = foo # Available for all targets 

Or export variables for a specific purpose ( target specific variable ):

 my-target: export MY_VAR_1 = foo my-target: export MY_VAR_2 = bar my-target: export MY_VAR_3 = baz my-target: dependency_1 dependency_2 echo do something 



You can also specify the target .EXPORT_ALL_VARIABLES for - you guessed it! - EXPORT ALL THE THINGS !!!:

 .EXPORT_ALL_VARIABLES: MY_VAR_1 = foo MY_VAR_2 = bar MY_VAR_3 = baz test: @echo $$MY_VAR_1 $$MY_VAR_2 $$MY_VAR_3 

see .EXPORT_ALL_VARIABLES

+37
Mar 28 '18 at 0:41
source share

I needed only local environment variables to call my test command, here is an example that sets up several vars environments in the bash shell and avoids the dollar sign in make .

 SHELL := /bin/bash .PHONY: test tests test tests: PATH=./node_modules/.bin/:$$PATH \ JSCOVERAGE=1 \ nodeunit tests/ 
+12
Apr 21 '17 at 22:38 on
source share



All Articles