49

I have a makefile where I want to read module name from input and then make directory based on it`s 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?

thanks for your answers

armezit
  • 649
  • 1
  • 7
  • 20

1 Answers1

103

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
Beta
  • 96,650
  • 16
  • 149
  • 150
  • 16
    You can also use [`.ONESHELL`](https://www.gnu.org/software/make/manual/html_node/One-Shell.html) to effectively put all the commands on the same line. – Brian M. Hunt Sep 18 '18 at 14:32