-2

How do I properly escape a the shell expansion syntax $() in a Makefile?

.PHONY: test
test:
        @if [ `find myDir -type f -not -name '*.openapi.yaml' | wc -l ` != 0 ]; then \
                echo "All files must end with '.openapi.yaml'";                           \
                find myDir -type f -not -name '*.openapi.yaml';                      \
                exit 42;                                                                  \
        fi

When I try modify that to,

@if [ $( find myDir -type f -not -name '*.openapi.yaml' | wc -l ) != 0 ]; then \

I get,

/bin/sh: line 0: [: !=: unary operator expected

If I try to escape the $ with \$ I get the same error,

4

You need double $:

.PHONY: test test1 test2

test: test1 test2

test1:
    @echo test1; if [ $$(echo 0) == "0" ]; then echo true; else echo false; fi

test2:
    @echo test2; if [ $$(echo 0) == "1" ]; then echo true; else echo false; fi

And an example run:

$ make
test1
true
test2
false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.