Does anyone know how to replace all spaces in a string in to a new line in a Makefile(GNU make)
So, you can format your makefiles for readability by adding newlines into the middle of a statement: you do this by escaping the internal newlines with a backslash ( \ ) character.
text := hello a b c
null :=
space := ${null} ${null}
${space} := ${space}# ${ } is a space. Neat huh?
define \n
endef
$(error [$(subst ${ },${\n},${text})])
It's probably easier -- and cleaner -- to use GNU Make's shell
function with sed
to do the replacement, rather than trying to do it entirely within make
.
STRING := foo bar baz
SPLIT := $(shell echo "${STRING}" | sed -e 's/ /\n/g')
Or, slightly better, if your shell is bash
instead of the default sh
:
STRING := foo bar baz
SPLIT := $(shell sed -e 's/ /\n/g' <<< ${STRING})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With