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/
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.

  • Can you try something like: "alias mycd='cd some_dir';mycd"
    – alpert
    Aug 18, 2014 at 13:49
  • That doesn't work: /bin/sh: mycd: command not found
    – Ixx
    Aug 18, 2014 at 13:51
  • I found the solution: stackoverflow.com/questions/1789594/… had to add a ; after the cd and then execute the script in the same line.
    – Ixx
    Aug 18, 2014 at 13:54

3 Answers 3


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:

    git clone http://somerepo ~/some_dir
    cd ~/some_dir/ && python myscript.py install
  • 1
    Also see this post for an updated answer.
    – mloning
    Feb 26, 2020 at 9:10
  • Updated answer on this very same page, see below.
    – NicoKowe
    Jan 25, 2023 at 15:20

since GNU make version 3.82 you can add .ONESHELL: on the top of your Makefile and then cd folder will do the trick as you expect. Read the docs


A script is run in a subshell, which means it has its own $PWD. Once it exits you get the "old" $PWD. If you want to keep the $PWD you need to source (aka. .) the script instead of running it. This won't work in a Makefile, however, because each command is run in a separate subshell:

$ pwd
$ cat test.sh
cd /
$ cat Makefile
    . ./test.sh && pwd
$ make test
. ./test.sh && pwd

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .