How to get a shell environment variable in a makefile?

后端 未结 3 655
悲哀的现实
悲哀的现实 2020-12-12 21:40

In shell when I enter

echo $demoPath

it prints

/usr/local/demo

How can I get the value of this variable <

相关标签:
3条回答
  • If you've exported the environment variable:

    export demoPath=/usr/local/demo
    

    you can simply refer to it by name in the makefile (make imports all the environment variables you have set):

    DEMOPATH = ${demoPath}    # Or $(demoPath) if you prefer.
    

    If you've not exported the environment variable, it is not accessible until you do export it, or unless you pass it explicitly on the command line:

    make DEMOPATH="${demoPath}" …
    

    If you are using a C shell derivative, substitute setenv demoPath /usr/local/demo for the export command.

    0 讨论(0)
  • 2020-12-12 22:16
    all:
        echo ${PATH}
    

    Or change PATH just for one command:

    all:
        PATH=/my/path:${PATH} cmd
    
    0 讨论(0)
  • 2020-12-12 22:19

    for those who want some official document to confirm the behavior

    Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment. (If the ‘-e’ flag is specified, then values from the environment override assignments in the makefile.

    https://www.gnu.org/software/make/manual/html_node/Environment.html

    0 讨论(0)
提交回复
热议问题