Pass a Make variable to Bashscript that is invoked from inside Make Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Pass a Make variable to Bashscript that is invoked from inside Make without wasting too much if your time.

The question is published on by Tutorial Guruji team.

In a pattern rule I would like to export make variable like $(TargetLocation), $@.

I tried the following but It didn’t get exported. The bash script is executable.

export $(TargetLocation); 
export $@; 
./EditHtml.sh;

I also tried this (I have used ${VarA} instead of ${TargetLocation} inside bash script), but it didn’t get exported:

./EditHtml.sh VarA=$(TargetLocation); 

I would like to use these variables in the Bash script. Any suggestions?

Answer

You need to export the variable name, not the value.

You can either export the name for all sub processes with the GNU Make export statement

export TargetLocation

If you want the value just for one rule, then your statement must be regular shell syntax

    export TargetLocation="$(TargetLocation)"; 
    ./EditHtml.sh

In this case, depending on the content of the variable, quotes may be necessary, and it would still be vulnerable if there may be arbitrary values for the variable. On the other hand, if make runs with the privileges of the invoking user, there is no additional danger.

We are here to answer your question about Pass a Make variable to Bashscript that is invoked from inside Make - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji