7

Each command in Makefile is executed by a separate shell.

What is the reason for this? Why commands aren't executed as in a regular script? I am aware that it is possible to use one shell using .ONESHELL special target.

But what are the reasons for default behavior?

2

I suspect the reason is that "make" normally halts if there's an error result. So if you have 3 separate commands, it may be wise to halt immediately if command #1 fails.

1

I can't tell you the rationale of the make makers, but one shell per recipe line makes sense to me in that there is only one state to be kept and modified and that's the state in the make process. Also, one fresh shell per line ensures that each line does the same thing every time it is executed. If it always were the same shell recipe lines and even entire recipies might have side effects on each other and the make process has virtually no method of detecting what is desireable and what is not ... let alone controling that behaviour. A shell per line makes every line atomic and program flow is returned to make, which gives a lot more power to make.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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