As mentioned above the expansion of a variable depends on where it is being expanded.
Makefile:
$(VAR)
and ${VAR}
will expand to the value stored in VAR
(which will be var
)
$VAR
will expand to the value stored in V
followed by AR
(so, unless you specified a variable named V
, it will expand to the literal AR
-- just mentioning it here as this is a common mistake people make)
$${VAR}
or $$VAR
will expand to the literals ${VAR}
and $VAR
respectively. This can be used to defer the expansion of the variable until bash is running (see below for an example)
bash or sh (or within a makefile recipe line):
$VAR
or ${VAR}
will expand to the value stored in the bash variable VAR
$(VAR)
will run the command VAR
Example:
Makefile:
VAR1 := var1
foo:
echo var1=$(VAR1)
VAR2=var2; echo $$VAR2
VAR3="echo running var3"; echo $$($$VAR3)
when foo is run, the first recipe line will be expanded to echo var1=var1
by make, and this will be passed to the shell, and the shell will output var1=var1
The second recipe line will run, and make will pass VAR2=var2; echo var2=$VAR2
to the shell, and the shell will output var2=var2
. Note that you have to access a bash variable from the same recipe line as it was set in -- the value will not persist to the next recipe line.
The last recipe line will pass VAR3="echo running var3"; echo $($VAR3)
to the shell. From the shell, $VAR3
is expanded to echo running var3
, and then $(echo running var3)
is run as a command, outputting running var3
, which is then echoed again.