/ Published in: Makefile

Make (and GMake) ignore spaces in many constructs, making it difficult to use a single space as a search string or a replacement string, for example. This trick works around the problem by assigning the space to a variable named SPACE and using that instead. So given this makefile:
NOTHING:=
SPACE:=$(NOTHING) $(NOTHING)
NAME_WITH_UNDERSCORES:=$(subst $(SPACE),_,$(NAME))
print : ; @echo $(NAME_WITH_UNDERSCORES)
The command
gmake NAME="Professor Hubert Farnsworth"
would print
Professor_Hubert_Farnsworth
NOTHING:=
SPACE:=$(NOTHING) $(NOTHING)
NAME_WITH_UNDERSCORES:=$(subst $(SPACE),_,$(NAME))
print : ; @echo $(NAME_WITH_UNDERSCORES)
The command
gmake NAME="Professor Hubert Farnsworth"
would print
Professor_Hubert_Farnsworth
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
NOTHING:= SPACE:=$(NOTHING) $(NOTHING)
Comments
