If foo
is a make variable
From the Text functions section of the GNU make manual:
$(subst from,to,text)
performs a textual replacement on the text
text: each occurrence of from is replaced by to. The result is
substituted for the function call.
So:
$(subst :, ,$(foo))
splits the content of make variable foo
by replacing all :
by a space. Still from GNU make manual:
$(lastword names…)
The argument names is regarded as a series of
names, separated by whitespace. The value is the last name in the
series.
So:
$(lastword $(subst :, ,$(foo)))
should do what you want. Demo (host>
is the shell prompt):
host> cat Makefile
foo := 1:2:3:4:5
all:
$(info $(lastword $(subst :, ,$(foo))))
host> make
5
If foo
is a shell variable
You must:
- Protect the
$
signs of your recipe from the first expansion that make performs before passing the recipe to the shell,
- Consider that each line of your recipe is executed by a separate shell: if you define a shell variable on a line and use it on another one, it does not work as you would expect.
all:
@foo=1:2:3:4:5 && \
echo $${foo##*:}
should do what you want: the $
sign is properly escaped and it is a single-line recipe (thanks to the trailing \
). Demo:
host> cat Makefile
all:
@foo=1:2:3:4:5 && \
echo $${foo##*:}
host> make
5