Don't stop make'ing if a command fails, but check exit status

Each update command in a Makefile rule is executed in a separate shell. So $? does not contain the exit status of the previous failed command, it contains whatever the default value is for $? in a new shell. That's why your [ $? -eq 0 ] test always succeeds.


You don't need the test of $? since && works if $? is zero and || is proceed in case of a non-zero return value.

And you don't need the minus since at the return value to make is taken from the last proceed program call of the line. So this works fine

failure:

      @/bin/false && echo "success!" || echo "failure!" 

success:

      @/bin/true && echo "success!" || echo "failure!"

The opposite happens: If you wanna do your own message and want to break the make process anyway with a non-zero value, you need to write something like this:

failure:

      @/bin/false && echo "success!" || { echo "failure!"; exit 1; }

From the GNU make documentation:

When errors are to be ignored, because of either a ‘-’ or the ‘-i’ flag, make treats an error return just like success, except that it prints out a message that tells you the status code the shell exited with, and says that the error has been ignored.

To utilize make's exit status in a case like this, execute make from a script:

#!/bin/bash
make
([ $? -eq 0 ] && echo "success!") || echo "failure!"

And have your Makefile contain:

all:
    /bin/false

Tags:

Gnu Make

Make