[pm-h] use of $$ versus $ in makefile and bash

G. Wade Johnson gwadej at anomaly.org
Thu Aug 30 16:50:11 PDT 2007


On Thu, 30 Aug 2007 15:33:57 -0500
"Russell L. Harris" <rlharris at oplink.net> wrote:

> * G. Wade Johnson <gwadej at anomaly.org> [070830 07:28]:
> > On Wed, 29 Aug 2007 17:12:55 -0500
> > "Russell L. Harris" <rlharris at oplink.net> wrote:
> > 
> >> I am puzzled concerning the use of the symbols "$" and "$$" in a
> >> makefile.  I do not understand why "$$" is required in some
> >> instances, and "$" in other instances.
> > 
> > According to "Managing Project with make" by Oram and Talbott, $$ in
> > make causes a $ to be used in the command instead of being evaluated
> > as part of a make variable.
> 
> Thanks for an explanation which makes sense; the GNU Make manual is
> ambiguous, to say the least:
> 
>     "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." (from section
>     6.1)
> 
> The authors of such manuals seem never to test the manual by giving it
> to someone unfamiliar with the subject and asking him to flag passages
> which are not clear.

It is hard finding people to proofread all of the documentation.

> >> EXAMPLE 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> >> 
> >>     -(for d in ${SYMLINKDIR};                            \
> >>      do                                                  \
> >>          pushd $$d &&                                    \
> >>              ln -sf $(addprefix ../,${SYMLINKTARGET}) .; \
> >>          popd;                                           \
> >>      done;)
> 
> In the case of a variable name for which there is no possibility of
> expansion (or there is such a thing?), would not the following be
> equivalent?
> 
>     -(for d in ${SYMLINKDIR};                          \
>     do                                                 \
>         pushd $(d) &&                                  \
>            ln -sf $(addprefix ../,${SYMLINKTARGET}) .; \
>         popd;                                          \
>     done;)

It's perfectly legal to have a variable whose value is an empty
string. To make matters worse, you can define make variables on the
command line. So, how would make know whether or not $d might refer to a
variable that just isn't defined on this run.

And, if you treated it differently depending on whether or not it was
defined, there would be even nastier bugs to find. Using '$$' as an
escape for '$' appears to be the lesser evil.

G. Wade

-- 
Results are what you wanted, consequences are what you got.
                                                 -- Michael VanDusen


More information about the Houston mailing list