4

Is it possible to "disable" variable expansion in my Makefile for a certain section?

Here's an example of the issue I'm having:

print_command:
    @echo '$(COMMAND)'

And here's the output I'm getting:

$ export COMMAND='My favourite shell is $SHELL'
$ make print_command
My favourite shell is HELL
$ make print_command COMMAND='Welcome to $SHELL'
Welcome to HELL

And what I would like to get:

$ export COMMAND='My favourite shell is $SHELL'
$ make print_command
My favourite shell is $SHELL
$ make print_command COMMAND='Welcome to $SHELL'
Welcome to $SHELL

Is it possible to do this without using a double dollar like so:

$ export COMMAND='My favourite shell is $$SHELL'
$ make print_command
My favourite shell is $SHELL
$ make print_command COMMAND='Welcome to $$SHELL'
Welcome to $SHELL

In it's simplest form I'm looking to forward the exact contents of the variable COMMAND without make mangling it.

Inian
  • 80,270
  • 14
  • 142
  • 161
Jack Wilsdon
  • 6,706
  • 11
  • 44
  • 87

1 Answers1

8

Add a $$ double-dollar and double-quote it.

print_command:
                @echo "$$COMMAND"

Like,

$ export COMMAND='My favourite shell is $SHELL'; make print_command
My favourite shell is $SHELL

Check out this How to Use Variables page from the GNU make page.

If you just add a single-quote, make literally prints out the variable:- e.g. with

print_command:
                @echo '$$COMMAND'
$ export COMMAND='My favourite shell is $SHELL'; make print_command
$COMMAND

Because $ carries a special meaning in Makefile and that needs to be escaped. If you need make to expand the value for the variable defined, use a value syntax as

print_command:
                @echo $(value COMMAND)
$ export COMMAND='My favourite shell is $SHELL'; make print_command
My favourite shell is /bin/bash

In the above case, the value of environment variables being expanded to /bin/bash.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 2
    Is it possible to pass it on the command line without `export` (i.e. after `make` but still without a double dollar? I'd assume it's not, as it's the equivalent of having the definition in the `Makefile` itself. – Jack Wilsdon Aug 04 '16 at 14:23