How I could add dir to $PATH in Makefile? How I could add dir to $PATH in Makefile? linux linux

How I could add dir to $PATH in Makefile?


Did you try export directive of Make itself (assuming that you use GNU Make)?

export PATH := bin:$(PATH)test all:    x

Also, there is a bug in you example:

test all:    PATH=bin:${PATH}    @echo $(PATH)    x

First, the value being echoed is an expansion of PATH variable performed by Make, not the shell. If it prints the expected value then, I guess, you've set PATH variable somewhere earlier in your Makefile, or in a shell that invoked Make. To prevent such behavior you should escape dollars:

test all:    PATH=bin:$$PATH    @echo $$PATH    x

Second, in any case this won't work because Make executes each line of the recipe in a separate shell. This can be changed by writing the recipe in a single line:

test all:    export PATH=bin:$$PATH; echo $$PATH; x


By design make parser executes lines in a separate shell invocations, that's why changing variable (e.g. PATH) in one line, the change may not be applied for the next lines (see this post).

One way to workaround this problem, is to convert multiple commands into a single line (separated by ;), or use One Shell special target (.ONESHELL, as of GNU Make 3.82).

Alternatively you can provide PATH variable at the time when shell is invoked. For example:

PATH  := $(PATH):$(PWD)/bin:/my/other/pathSHELL := env PATH=$(PATH) /bin/bash


Path changes appear to be persistent if you set the SHELL variable in your makefile first:

SHELL := /bin/bashPATH := bin:$(PATH)test all:    x

I don't know if this is desired behavior or not.