Contents|Index|Previous|Next
Basics of variable references

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. This special significance of ‘$’ is why you must write ‘$$’ to have the effect of a single dollar sign in a file name or command. Variable references can be used in any context: targets, dependencies, commands, most directives, and new variable values. The following is an example of a common case, where a variable holds the names of all the object files in a program.
 

Variable references work by strict textual substitution. Thus, the following rule could be used to compile a C program, ‘prog.c’.
 

Since spaces before the variable value are ignored in variable assignments, the value of foo is precisely ‘c’. (Don’t actually write your makefiles this way!) A dollar sign followed by a character other than a dollar sign, open-parenthesis or open-brace treats that single character as the variable name. Thus, you could reference the variable, x, with ‘$x’. However, this practice is strongly discouraged, except in the case of the automatic variables (see Automatic variables).


Top|Contents|Index|Previous|Next