Shell – What are the reasons for each line in Makefile to be executed in a separate shell

makeshell

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?

Best Answer

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.