Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
943 views
in Technique[技术] by (71.8m points)

bash - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...