Escaping in makefile

ShellMakefileEscaping

Shell Problem Overview


I'm trying to do this in a makefile and it fails horribly:

M_ARCH := $(shell g++ -dumpmachine | awk '{split($1,a,"-");print a[1]}')

do you know why? I guess it has to do with escaping, but what and where?

Shell Solutions


Solution 1 - Shell

It's the dollar sign, in makefiles you'll have to type $$ to get a single dollar sign:

M_ARCH := $(shell g++ -dumpmachine | awk '{split($$1,a,"-");print a[1]}')

Solution 2 - Shell

Make is quite lispy when you get down to it. Here's a non-awk version that does the same thing:

space := $() #

M_ARCH := $(firstword $(subst -,$(space),$(shell g++ -dumpmachine)))

all:
	$(info $(M_ARCH))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonas ByströmView Question on Stackoverflow
Solution 1 - ShellMartinView Answer on Stackoverflow
Solution 2 - ShellrichqView Answer on Stackoverflow