Question

After an expression:

command1 | command2

I can retrieve the first commands exit status through ${PIPESTATUS[0]}

Is there an equivalent for expressions in the form:

command3 && command4 || command5

I would like to retrieve the exitcode of command3:

command3 && command4 || command5
retval=${SHORTCIRCUIT[0]}     # how to get this value?

Preferably without creating temporary files.

Was it helpful?

Solution

Elaborating devnull's approach leads to

{ c3=0; command3 || c3=$? && false; } && command4 || command5
echo $c3

. Because every command can be replaced by

{ c=0; command || c=$? && false; }

(provided that c is initially unset if executed conditionally), this is easily extensible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top