How do I write the 'cd' command in a makefile?


Starting from GNU make 3.82, you can use the .ONESHELL special target to run all recipe lines in a single instantiation of the shell (bold emphasis mine):

  • New special target: .ONESHELL instructs make to invoke a single instance of the shell and provide it with the entire recipe, regardless of how many lines it contains. [...]
    cd ~/some_dir
    pwd # Prints ~/some_dir if cd succeeded

For example, I have something like this in my makefile:

     cd some_directory

But when I typed make I saw only 'cd some_directory', like in the echo command.

What do you want it to do once it gets there? Each command is executed in a subshell, so the subshell changes directory, but the end result is that the next command is still in the current directory.

With GNU make, you can do something like:

    $(shell cd $(BIN); ls)