Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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