0

I've the following makefile:

.ONESHELL:

SHELL := /bin/bash

build-%:
        @[ $(findstring -, $*) ] && DIR_ENV=$(subst -,/,$*) || DIR_ENV=$*
        @echo ${DIR_ENV}

I'm trying to have available the DIR_ENV but without no luck. I know that every command executed is executed in its own shell so no sharing of variavbles. However I've added ONESHELL directive. But it still doesn't work. What am I'm missing?

1
  • 1
    1) Do you want DIR_ENV available in other commands withing that rule, or in other rules? 2) Which version of Make are you using? 3) What happens when you put the echo command in the first line (e.g. DIR_ENV=foo; echo ${DIR_ENV})? 4) have you tried doubling the '$' (echo $${DIR_ENV})? Commented Mar 22, 2018 at 16:58

1 Answer 1

2

You are missing the fact the string ${DIR_ENV} is evaluated by make first and the resulting value is a null string. Use this

build-%:
        @[ $(findstring -, $*) ] && DIR_ENV=$(subst -,/,$*) || DIR_ENV=$*
        @echo $${DIR_ENV}

Also the Make syntax

$(if $(findstring -,$*),$(subst -,/,$*),$*)

Does the if/then/else logic in Make, not the shell

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking over lunch the entire if/then/else can be replaced with a single @echo '$(subst -,/,$*)' statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.