2

I have a bash script (like A.sh) which includes a variable like:

ToBeRead=$(sed -n '$=' LogFile)

I was wondering how can I use this variable in a makefile which is called from A.sh like:

make -f Makefile SayHello

2 Answers 2

4

You can explicitly set make variables:

make ToBeRead:="$(sed -n '$=' Logfile)" SayHello
Sign up to request clarification or add additional context in comments.

Comments

1

Forgive me if this is a dumb noob answer, but couldn't you use an environment variable?

In A.sh:

export VARIABLE=value

In makefile:

 # sets VARIABLE if not already set
 VARIABLE?=other_value

I could be wrong I'm new to makefiles as well.

4 Comments

Actually tried this one, but seems variables' life time is limited to the execution of script, unless you make them permanent.
Ah, I see. So this is a dumb noob answer ;)
The A.sh script invokes make and make inherits a copy of the exported environment from its parent. Regardless of when A.sh exits (although in your case here it won't exit until after make exits anyway), make still has this copy. I expect that the mistake the questioner made was not exporting the variable. If the variable is not exported, then it's not copied to child programs. So no, @Will, this is not a dumb noob answer: it will work as you've described it here.
Thanks for the clarification @MadScientist, very helpful. The export keyword is something I've only used once or twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.