Phi Quang Phuoc
Phi Quang Phuoc

Reputation: 243

addprefix in MAKE doesn't work correctly when -e is used as prefix

I encounter a strange behavior of addprefix with -e. The expected -e for the first word is not added. Here's the makefile to reproduce it.

a := file1 file2 file3
b := $(addprefix -s , $(a))
c := $(addprefix -e , $(a))
all:
    @echo $(b)
    @echo $(c)

Result:

-s file1 -s file2 -s file3
file1 -e file2 -e file3

Is it a bug or something is missing?

Upvotes: 0

Views: 169

Answers (1)

MadScientist
MadScientist

Reputation: 100956

You should never use echo to print any string that is not just simple text that you know contains no starting dash characters. The echo command is not really standardized and there are a lot of versions, some of which accept options and some of which don't.

On your system, the echo command accepts a -e option so when you use echo -e file -e file2 -e file3 echo eats the first -e.

You can use:

all:
        @printf '%s\n' '$(b)'
        @printf '%s\n' '$(c)'

to be portable. Or you can use make's built-in functions:

all:
        $(info $(b))
        $(info $(c))

Upvotes: 2

Related Questions