In Unix, can I run 'make' in a directory without cd'ing to that directory first?

后端 未结 5 1202
后悔当初
后悔当初 2021-01-29 18:42

In Unix, can I run make in a directory without cd\'ing to that directory first?

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 19:18

    As noted in other answers, make(1) has a -C option for this; several commands have similar options (e.g. tar). It is useful to note that for other commands which lack such options the following can be used:

    (cd /dir/path && command-to-run)
    

    This runs the command in a sub-shell which first has its working directory changed (while leaving the working directory of the parent shell alone). Here && is used instead of ; to catch error cases where the directory can not be changed.

提交回复
热议问题