Monday, June 6, 2011

Neat trick to replace spaces with any other character in Makefile

Sometimes, you would want to replace spaces in a string with another character (for this post let's assume that this character is a colon ':'. You can replace it with whatever replacement string you desire). The following makefile trick serves this purpose:
null      :=
SPACE     := $(null) $(null)
WITHSPACE := A string with spaces
REPLACED  := $(subst $(SPACE),:,$(WITHSPACE))
default:
 @echo WITHSPACE = $(WITHSPACE)
 @echo REPLACED = $(REPLACED)
And here's the output:
$ make
WITHSPACE = A string with spaces
REPLACED = A:string:with:spaces

No comments:

Post a Comment