-1

I need to run make file_name command in a subdirectory using shell script and then cd .. out of that subdirectory and run the remaining commands in the main directory.

1
  • And what prevents you from doing so? May 30, 2019 at 12:13

2 Answers 2

3

You can run make from the parent directory without switching to the subdirectory first:

make -C subdir file_name
1
  • 1
    This is a good solution if you know that your make is GNU make. Not all versions of make support the -C option. May 30, 2019 at 16:16
2

Assuming the script is initially running in "the main directory", just cd to the subdir in a subshell. eg

#!/bin/sh

# do stuff in main directory
( cd subdir; make file_name ) # use a subshell 
# now run more commands in the main directory

You can also use pushd and popd, or try storing $(pwd), or just use cd .., but the subshell is usually the cleanest solution.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.