On Wed, 2022-01-19 at 09:50 +0800, Hongyi Zhao wrote: > So, I would like to know when I should use () to group some commands > and when not.
Your question is about how to write shell scripts, not about how to write makefiles. The recipe of a makefile is a shell script. You should use (), which (in this situation) is a shell script construct, when you would want to use a subshell (which is what this syntax provides) in a shell script. It has nothing at all to do with make. The help-make mailing list is probably not the best place to learn how to write shell scripts or understand them. > But see the following testings: > > werner@X10DAi-00:~$ /bin/sh -c '(exit 1); echo $?' > 1 > werner@X10DAi-00:~$ /bin/sh -c 'exit 1; echo $?' > werner@X10DAi-00:~$ > > Why must I use () here, otherwise, the exit code will not be > captured? Again, this is a shell script question (you can tell, because you are not running make at all here) and your best bet is to find a group or manual or tutorial that can help you understand shell scripting. It's not correct to say it "will not be captured". It is captured. For example you can run: $ /bin/sh -c 'exit 1; echo $?' $ echo $? 1 What's correct is that it is not PRINTED. The "exit" command in a shell script exits from the shell script. So when you run: /bin/sh -c 'exit 1; echo $?' the exit command causes the shell script to exit when it runs the exit command. So, it never reaches the echo command because it exited first. The () adds a subshell, so when you run: /bin/sh -c '(exit 1); echo $?' that's similar to running this: /bin/sh -c '/bin/sh -c "exit 1"; echo $?' Now the exit command exits from the inner /bin/sh but not the outer /bin/sh, so then the outer /bin/sh runs the echo command.