Home > Backend Development > Golang > How Do I Escape Expression Interpolation in Makefiles to Execute Commands Correctly?

How Do I Escape Expression Interpolation in Makefiles to Execute Commands Correctly?

Barbara Streisand
Release: 2024-12-25 03:27:11
Original
550 people have browsed it

How Do I Escape Expression Interpolation in Makefiles to Execute Commands Correctly?

Escaping Expression Interpolation in Makefiles

When writing Makefiles, it's common to want to execute commands that incorporate expressions. A common example of this is in testing, where you might want to run tests selectively based on certain criteria.

Suppose you're working on a Go project and want to exclude tests from the vendor directory. You could easily do this in the command line using:

$ go test $(go list ./... | grep -v /vendor/)
Copy after login

However, when you try to incorporate this into a Makefile:

test:
    go test $(go list ./... | grep -v /vendor/)

.PHONY: test
Copy after login

You'll notice that the expression is not interpolated:

$ make
go test
?       github.com/m90/some-repo    [no test files]
Copy after login

To resolve this issue, you need to escape the "$" character in the expression using a second "$":

test:
    go test $$(go list ./... | grep -v /vendor/)

.PHONY: test
Copy after login

By escaping the "$", you're instructing the Makefile to interpret the expression as a literal command to be executed, rather than trying to substitute the variable.

The above is the detailed content of How Do I Escape Expression Interpolation in Makefiles to Execute Commands Correctly?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template