0
$ ls /tmp/foo
file1.txt

$ ls /tmp/bar
file5.txt  file7.txt

Makefile content:

$ cat Makefile 
NUMS = $(patsubst file%,%,$(basename $(notdir $(wildcard /tmp/foo/file*.txt /tmp/bar/file*.txt))))

all:
        @echo $(NUMS)

When I execute make.

$ make
1 5 7

how to I make it print numbers as comma separated, e.g: 1,5,7 ?

rodee
  • 3,233
  • 5
  • 34
  • 70
  • Just to point out that this question is more complicated than the one it's marked as a duplicate of: that one wants to replace spaces with a colon and this one wants to replace spaces with a comma. A comma needs more thought than a colon because commas are special to the make parser when expanding functions. Colons are not special. – MadScientist Jul 23 '17 at 13:02

1 Answers1

3

The important thing to understand about makefile parsing rules, which are different than some other languages like the shell, is that make breaks up arguments BEFORE it expands variables.

The good news is this means you can put any special character you like, by "hiding" it behind a variable:

COMMA := ,
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)

NUMS = $(subst $(SPACE),$(COMMA),$(basename $(notdir $(wildcard /tmp/foo/file*.txt /tmp/bar/file*.txt))))
MadScientist
  • 92,819
  • 9
  • 109
  • 136