Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8528101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:43:39+00:00 2026-06-11T08:43:39+00:00

I am basically looking into a buggy makefile, which confused me with the following

  • 0

I am basically looking into a buggy makefile, which confused me with the following scenario.

default:
     echo "Build started with $@ at $(ENV_VAR)";\
     ENV_VAR=$(NEW_DIR); \
     cd $(ENV_VAR); \
     $(MAKE);

Objective of the programmer:

Basically the programmer has intended to override the environment variable ENV_VAR with a new value (path to a directory) NEW_DIR and then execute make recursively within that directory.

Bug in this makefile:

The bug that I see is the coder has used “cd $(ENV_VAR)” instead of “cd $$(ENV_VAR)”. So because of this, we haven’t actually used the overriden make variable for “cd” command instead still using the value of environment variable before executing $(MAKE). Also the cd command failed because of non-existing path.Finally we end up doing a recursive make.

Questions I have

As I mentioned in the above paragraph, we are in the scenario of recursive make. Interestingly What I see is when the same target is called for the second time I see the $(ENV_VAR) to reflect the overridden value of NEW_DIR.That’s making me puzzled.

Sample Output:

Assume:

NEW_DIR=/new-dir OLD-DIR=/old-dir

Output:

Build started with default at /old-dir
bin/sh /old-dir: no such file (or) directory
Build started with default at /new-dir

Any suggestions on this please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:43:40+00:00Added an answer on June 11, 2026 at 8:43 am

    You’re right that $(ENV_VAR) is incorrect. But $$(ENV_VAR) is incorrect too.

    Make expands the variables in the command, then passes the command to the shell, which does its own expansion. So

    default:
         echo "Build started with $@ at $(ENV_VAR)";\
         ENV_VAR=$(NEW_DIR); \
         cd $(ENV_VAR); \
         $(MAKE);
    

    becomes a shell command

    echo "Build started with $@ at /old_dir";\
    ENV_VAR=/new_dir; \
    cd /old_dir; \
    make
    

    This command has not yet been executed; the assignment has not yet taken place, and it’s already too late.

    The proposed solution won’t work either. This:

    default:
         echo "Build started with $@ at $(ENV_VAR)";\
         ENV_VAR=$(NEW_DIR); \
         cd $$(ENV_VAR); \
         $(MAKE);
    

    becomes this:

    echo "Build started with $@ at /old_dir";\
    ENV_VAR=/new_dir; \
    cd $(ENV_VAR); \
    make
    

    This looks good, but notice that cd $(ENV_VAR); is Make syntax, not shell syntax (at least not bash). The correct shell syntax is cd $ENV_VAR, so the rule should be:

    default:
         echo "Build started with $@ at $(ENV_VAR)";\
         ENV_VAR=$(NEW_DIR); \
         cd $$ENV_VAR; \
         $(MAKE);
    

    According to the manual, “by default, only variables that came from the environment or the command line are passed to recursive invocations” (but you can override this behavior if you choose). Since ENV_VAR is an environmental variable, it will be passed to the sub-make, and if you modify it in the command shell, the sub-make will receive the modified value.

    Finally, I suggest you try it this way:

    default:
        echo "Build started with $@ at $(ENV_VAR)";
        Make -C $(NEW_DIR) ENV_VAR=$(NEW_DIR);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Evening all. I have the following code that I need looking into - basically
Basically, looking into the solutions to get a FlatWSDL, I found the following: http://weblogs.thinktecture.com/cweyer/2007/05/improving-wcf-interoperability-flattening-your-wsdl.html
I've started looking into multi-threading in Ruby. So basically, I want to create a
Basically, I'm looking for something that will allow me to replicate the following Perl
I am looking into writing a DLL in C# which as part of its
I've started looking into Appcelerator 1.8.0 few days and back and i'm a little
I've been looking into applying OO practices using c. Basically the outline I've gotten
I just started looking into ctypes and was a little curious on how they
I'm a Java/.NET developer but on my spare time I've started looking into WordPress
Thanks for looking into this. Basically, what I am trying to do is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.