Makefile 环境变量中的美元 [英] Dollars in Makefile environment variables

查看:29
本文介绍了Makefile 环境变量中的美元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在我的 Makefile 中为某个部分禁用"变量扩展?

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)'

这是我得到的输出:

$ 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

我想得到什么:

$ 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

我希望以最简单的形式转发变量 COMMAND 的确切内容,而不需要 make 对其进行修改.

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

推荐答案

添加一个 $$ 双美元并双引号.

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

print_command:
                @echo "$$COMMAND"

喜欢,

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

看看这个 GNU make 页面中的如何使用变量 页面.

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

如果你只是添加一个单引号,make 会从字面上打印出变量:-例如与

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

因为 $ 在 Makefile 中具有特殊含义,需要转义.如果您需要 make 来扩展已定义变量的值,请使用 value 语法 as

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

在上述情况下,环境变量的值被扩展为/bin/bash.

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

这篇关于Makefile 环境变量中的美元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆