Why current directory doesn’t change in makefile

cd-commandmake

I'm trying to run a simple script- clone a git repository into a certain directory, then cd to this directory in order to execute an installation script.

This script is in a Makefile.

But the cd seems not to be working. It doesn't find my installation script.

I added a pwd after the cd in the script, and it shows me the directory from where I'm executing the script, not the directory where I cd into.

What's the problem?

git clone http://somerepo ~/some_dir
cd ~/some_dir/
pwd
python myscript.py install

=>

pwd: /hereIsPathToDirectoryFromWhichIRunTheScript

python: can't open file 'setup.py': [Errno 2] No such file or directory

It also doesn't work with ./setup.py.

If I enter the absolute path ~/some_dir/setup.py the script fails later because it's trying to access resources in the same folder.

Best Answer

You're using a makefile. Makefiles aren't scripts, each line is executed in a new shell. Meaning when you change the environment in line (such as cd), that change is not propagated to the next line.

The solution is that when you want to preserve the environment between commands, you run all the commands in the same line. All the commands will then be executed in the same shell, and the environment is preserved.

For example:

target:
    git clone http://somerepo ~/some_dir
    cd ~/some_dir/ && python myscript.py install
Related Question