To summarize the other answers, for general use:
if [ foo ]; then
a; b
elif [ bar ]; then
c; d
else
e; f
fi
if [ foo ]; then a && b; elif [ bar ]; c && d; else e && f; fi
( foo && a && b ) || ( bar && c && d ) || e && f;
Remember that the AND and OR operators evaluate whether or not the result code of the previous operation was equal to true/success (0
). So if a custom function returns something else (or nothing at all), you may run into problems with the AND/OR shorthand. In such cases, you may want to replace something like ( a && b )
with ( [ a == 'EXPECTEDRESULT' ] && b )
, etc.
Also note that (
and [
are technically commands, so whitespace is required around them.
Instead of a group of &&
statements like then a && b; else
, you could also run statements in a subshell like then $( a; b ); else
, though this is less efficient. The same is true for doing something like result1=$( foo; a; b ); result2=$( bar; c; d ); [ "$result1" -o "$result2" ]
instead of ( foo && a && b ) || ( bar && c && d )
. Though at that point you'd be getting more into less-compact, multi-line stuff anyway.