makefilegnu-makebsdmake

GNU make equivalent to BSD make's $(var:Q)?


BSD make has a :Q variable expansion modifier, documented in the FreeBSD make man page as follows:

:Q   Quotes every shell meta-character in the variable, so that it can be
     passed safely through recursive invocations of make.

If variable var has value a b\c"d'e$f, then $(var:Q) expands to a\ b\\c\"d\'e\$f (or something equivalent). This is useful to pass strings to the shell without worrying that the shell will interpret any special characters.

Does GNU make have an equivalent? Or do I have to escape special characters my own?


Solution

  • GNU make provides functions subst and patsubst which can help solve the problem. Those are more general, but require more work by the developer since they do not solve the specific problem. Also, the documentation does not show they use regular expressions, adding to the work.

    For instance, you could in principle build up an expression like this:

    $(subst \\,\\\\,$(subst ",\", $(subst ',\', var)))
    

    For more discussion,