10

I am not sure whether I have described the question properly, but currently I am solving this problem in the following way

QUOTEDSTR := "hello world"
NORMALSTR := $(shell echo $(QUOTEDSTR))

Is there a more built-in way that 'make' can do this without calling shell? Thanks

Danqi Wang
  • 1,597
  • 1
  • 10
  • 28

4 Answers4

15

Another option:

NORMALSTR := $(patsubst "%",%,$(QUOTEDSTR))

Beta's answer will remove every quote in the string. The above solution will ONLY remove quotes that appear at the beginning and end. For example:

QUOTEDSTR := -DTITLE=\"Title\"

Beta's answer will result in a value of -DTITLE=\Title\ while using the patsubst solution this value will not be changed.

It depends on what you want.

EDIT

If you want to handle whitespace and still only match quotes at the beginning/end of the variable as per @stefanct's comment, you'll have to play some tricks. First you need to find a non-whitespace character which you know will never appear in your string. Let's choose ^ but you can choose something else if you want.

The algorithm is: convert all spaces to this character, then remove the quotes from the resulting single "word", then convert all instances of that character back to spaces, like this:

# Get a variable S that contains a single space
E :=
S := $E $E

NORMALSTR := $(subst ^,$S,$(patsubst "%",%,$(subst $S,^,$(QUOTEDSTR))))

Of course there are still complications; this handles only spaces for example, not other whitespace characters like TAB.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • 2
    A compressed version of your answer could be: NORMALSTR = $(QUOTEDSTR:"%"=%) that does basically the same. – dirac3000 Mar 31 '16 at 13:14
10

This should do it:

NORMALSTR := $(subst $\",,$(QUOTEDSTR))
Beta
  • 96,650
  • 16
  • 149
  • 150
2

All answers yet have issues. patsubst "[f]inds whitespace-separated words" so the simple solution by @MadScientist does not work for strings like "hello world". The one by @Beta on the other hand removes all quote characters no matter where they are.

The code below shows how to deal with strings with spaces in them as well. However, it will also remove other quote characters on the edge of words, for example "hello "world"3" will be transformed to hello world"3. If that's any better... I don't know, probably not.

Instead of the other solutions this one creates a user function named unquote instead of directly replacing the strings.

quoted="hello world"

unquote = $(patsubst "%,%,$(patsubst %",%,$(1)))
#unquote = $(subst $\",,$(1))
#unquote = $(patsubst "%",%,$(1))
#unquote = $(shell echo $(1))

unquoted = $(call unquote,$(quoted))

$(info Variable quoted is $(quoted))
$(info Variable unquoted is $(unquoted))

This simply looks for all quote characters at the start and end of every (white-space separated) word and removes it.

stefanct
  • 2,503
  • 1
  • 28
  • 32
0
NORMALSTR := $(subst $\",,$(QUOTEDSTR))

equal to

NORMALSTR := $(subst ",,$(QUOTEDSTR))

because you didn't define $\ variable, so it is null string

$\" avoid to syntax highlight issue in editor.

",,$(QUOTEDSTR)) will be recognized as a string by Editor until it find another ".

Galen Liu
  • 19
  • 3