nikhil chaubey
nikhil chaubey

Reputation: 401

Running a script inside a makefile

I want to run a script inside a makefile like this

    all: a b
    a:
        cd ~/trials; \
        . ./sx.sh
    b:
        echo $(bn)

sx.sh do this

    export bn=1

I don't see the variable in my terminal while issuing make command. My aim is to run a script before compiling my project for those script specific settings.

Upvotes: 5

Views: 5820

Answers (1)

You can't assume that the commands issued by make are all processed by the same instantiation of the shell. Make does not open a shell and feed it commands one-by-one and nor does it save the commands into a file and then feed it into a shell. It usually spawns a shell for each command like this:

$(SHELL) -c 'cd ~/trials; . ./sx.sh'

which means you cannot alter the environment and have it inherited by later commands.

The best way is to use make variables to store the specifics you wish to pass to the commands and use those variables in appropriate places.

Upvotes: 4

Related Questions