I'm running a command to find all make files in a directory and run make build.

I would like to get xargs to stop on the first make build that fails.

Here's the command I have so far.

$ find . -name "Makefile" | xargs dirname | xargs -I {} make -C {} build

The problem is that xargs keeps going even after a make build fails. When I check the final status code, here's what I see.

$ echo $?

And when I checked the man page. Here's what it says.

123 if any invocation of the command exited with status 1-125

Which makes sense because some of those make builds failed with exit 1 or something.

Is there a way to get xargs to stop on the first make build error?



If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input.

It would be possible to use a subshell to return 255:

find . -name "Makefile" | xargs dirname | \
  xargs -I {} sh -c 'make -C "$1" build || exit 255' sh {}
| improve this answer | |
  • Aaah, okay. I was thinking there would be some extra flag or something as part of xargs itself. This works too though! – 425nesp Mar 5 at 5:59

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.