Q:

check returncode in a most small (and elegant) way

how to make this work?
#!/bin/bash
# this code is ugly and does not work
check_if_failed() {
    echo "arg1: $1"
    echo "arg2: $2"
    if [ $1 -ne 0 ] ; then
        exit $1
    fi
}

CHECK="check_if_failed $? $LINENO"

true ; $CHECK
false ; $CHECK

# (edit)
if true ; then
    false ; $CHECK
fi

The goal is to have one very small command to check the returncode, so that I can append it to every command line in a simple way.

A:

I think what you're looking for is simply
some_command || exit

As in C, the || short-circuits, so its right-hand side only gets evaluated if the left-hand side evaluates to "false" (which is interpreted here as a nonzero return code).

