armezit
armezit

Reputation: 667

read input variable in makefile and set variable upon its name

I have a makefile where I want to read module name from input and then make directory based on its name. here is my code:

build:  
    @read -p "Enter Module Name:" module;  
    module_dir=./modules/$$module  
    mkdir -p $$module_dir/build;  

But after setting module_dir, it contains only ./modules/ (with no module name concatenated).

What is wrong in my code?

Upvotes: 51

Views: 46222

Answers (1)

Beta
Beta

Reputation: 99094

Each command runs in its own subshell, so variables can't survive from one command to the next. Put them on the same line and they'll work:

build:  
    @read -p "Enter Module Name:" module; \  
    module_dir=./modules/$$module; \
    mkdir -p $$module_dir/build

Upvotes: 106

Related Questions