Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 230
Jack Wilsdon Avatar asked Mar 11 '23 12:03

Jack Wilsdon


1 Answers

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.

like image 61
Inian Avatar answered Mar 19 '23 14:03

Inian