Jack Wilsdon Jack Wilsdon - 3 months ago 14
Bash Question

Dollars in Makefile environment variables

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.

Answer

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.

Comments