3

This is my Makefile:

SHELL=/bin/bash
.SHELLFLAGS = -e -o pipefail -c 

env:
    make -version | head -1

This is what I'm getting on Ubuntu 20.04.3:

make -version | head -1
GNU Make 4.2.1
make[1]: write error: stdout
make: *** [Makefile:4: env] Error 1

What's wrong?

1 Answer 1

2

I don't see this problem with GNU make 4.3, so I guess something was changed there to allow this to work.

To fix it you can try changing your rule to this:

env:
        make -version | cat | head -1

The error is because the head program closes its stdin when it's read the first line, then make complains because it fails to write to its stdout (because it was closed).

By introducing the cat here, the cat reads all the input from make without closing the pipe so make doesn't get any error.

PS. You should always, always use the variable $(MAKE) when invoking a sub-make, never the hardcoded make.

Sign up to request clarification or add additional context in comments.

6 Comments

Same error here with cat (exit status 141). Probably cat that itself closes its stdin before make wrote its output. It looks like a race condition because it sometimes fails, sometimes not. Replacing cat by { sleep 1; cat; } seems to solve it. Or, maybe more elegant, head -1 < <($(MAKE) -version).
Or, maybe even better, { make -version ||:; } | head -1.
The fancy <(...) won't work unless you force SHELL = /bin/bash as that's not a feature of the POSIX shell.
Sure. I should have mentioned it. But as the OP already states SHELL = /bin/bash and all this is about the pipefail bash option...
for me the upper is not working, instead just pipe stderr make --version 2>/dev/null | head -n1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.