简体   繁体   中英

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 :

$ echo $RANDOM
14522

This does not work:

$ make echo-random

With Makefile :

echo-random:
        echo $(RANDOM)

Some questions:

  • Does make use a shell to run commands?
  • Is it possible to tell shell to use bash ?
  • Can make somehow access bash special env variables?

You can invoke bash with the -c argument (that tells it the next argument is a command it has to run and exit):

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.

The answer by @axiac is good. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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