How to get a command return code in a short circuit expression

After the expression:

command1 | command2 

I can get the exit status of the first commands with ${PIPESTATUS[0]}

Is there an equivalent for expressions in the form:

 command3 && command4 || command5 

I would like to get the exit code of command 3:

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

Preferably without creating temporary files.

+4
source share
1 answer

Devnull method development leads to

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

. Since each command can be replaced by

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

(assuming c initially disabled if conditionally executed), it is easily extensible.

+2
source

Source: https://habr.com/ru/post/1492034/


All Articles