What's the difference between parenthesis $() and curly bracket ${} syntax in Makefile?

VariablesSyntaxMakefile

Variables Problem Overview


Is there any differences in invoking variables with syntax ${var} and $(var)? For instance, in the way the variable will be expanded or anything?

Variables Solutions


Solution 1 - Variables

There's no difference – they mean exactly the same (in GNU Make and in POSIX make).

I think that $(round brackets) look tidier, but that's just personal preference.

(Other answers point to the relevant sections of the GNU Make documentation, and note that you shouldn't mix the syntaxes within a single expression)

Solution 2 - Variables

The Basics of Variable References section from the GNU make documentation state no differences:

> To substitute a variable's value, write a dollar sign followed by the > name of the variable in parentheses or braces: either $(foo) or > ${foo} is a valid reference to the variable foo.

Solution 3 - Variables

As already correctly pointed out, there is no difference but be be wary not to mix the two kind of delimiters as it can lead to cryptic errors like in the GNU make example by unomadh.

From the GNU make manual on the Function Call Syntax (emphasis mine):

> […] If the arguments themselves contain other function calls or variable references, it is wisest to use the same kind of delimiters for all the references; write $(subst a,b,$(x)), not $(subst a,b,${x}). This is because it is clearer, and because only one type of delimiter is matched to find the end of the reference.

Solution 4 - Variables

The ${} style lets you test the make rules in the shell, if you have the corresponding environment variables set, since that is compatible with bash.

Solution 5 - Variables

Actually, it seems to be fairly different:

, = ,
list = a,b,c
$(info $(subst $(,),-,$(list))_EOL)
$(info $(subst ${,},-,$(list))_EOL)

outputs

a-b-c_EOL
md/init-profile.md:4: *** unterminated variable reference. Stop.

But so far I only found this difference when the variable name into ${...} contains itself a comma. I first thought ${...} was expanding the comma not as part as the value, but it turns out i'm not able to hack it this way. I still don't understand this... If anyone had an explanation, I'd be happy to know !

Solution 6 - Variables

It makes a difference if the expression contains unbalanced brackets:

${info ${subst ),(,:-)}}
$(info $(subst ),(,:-)))

->

:-(
*** insufficient number of arguments (1) to function 'subst'.  Stop.

For variable references, this makes a difference for functions, or for variable names that contain brackets (bad idea)

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
QuestionÉdouard LopezView Question on Stackoverflow
Solution 1 - VariablesNorman GrayView Answer on Stackoverflow
Solution 2 - VariablesÉdouard LopezView Answer on Stackoverflow
Solution 3 - VariablesAlexandre PerrinView Answer on Stackoverflow
Solution 4 - VariablesWarren MacEvoyView Answer on Stackoverflow
Solution 5 - VariablesunomadhView Answer on Stackoverflow
Solution 6 - VariablesErik CarstensenView Answer on Stackoverflow