简体   繁体   English

从makefile访问bash特殊变量

[英]Access bash special variable from makefile

I am running: 我在跑步:

» make --version
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

This works, from bash : 这从bash起作用:

$ echo $RANDOM
14522

This does not work: 这不起作用:

$ make echo-random

With Makefile : 使用Makefile

echo-random:
        echo $(RANDOM)

Some questions: 一些问题:

  • Does make use a shell to run commands? make是否使用shell运行命令?
  • Is it possible to tell shell to use bash ? 可以告诉shell使用bash吗?
  • Can make somehow access bash special env variables? 可以make某种方式访问bash特殊的env变量吗?

You can invoke bash with the -c argument (that tells it the next argument is a command it has to run and exit): 您可以使用-c参数调用bash (告诉bash下一个参数是它必须运行并退出的命令):

echo-random:
    @bash -c 'echo $$RANDOM'

This way, each invocation of make echo-random starts a new bash instance that runs the command echo $RANDOM and it produces the outcome you expect. 这样,每次调用make echo-random启动一个新的bash实例,该实例运行命令echo $RANDOM并产生您期望的结果。

The answer by @axiac is good. @axiac的答案很好。 This is an alternative: 这是一种替代方法:

SHELL = /bin/bash

random := $(shell echo $$RANDOM)

echo-random:
    echo $$RANDOM
    echo $(random)

Output: 输出:

» make
echo $RANDOM
18826
echo 16300
16300

See here and here 这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM