Conditionals in BASH

Rick Stevens ricks at nerd.com
Wed Apr 9 18:43:27 UTC 2008


Matthew Saltzman wrote:
> The conditionals && and || in bash are short-circuit, so that once a
> term is evaluated that determines the success or failure of the entire
> expression, evaluation of the remaining alternatives is skipped.
> 
> In a test expression (enclosed in [ ... ]), are the conditionals -a and
> -o also short-circuit?  The O'Reilly BASH book seems to suggest that
> they are not, but the description seems to me to be ambiguous.

To my knowledge, all conditionals are evaluated left-to-right.  As
soon as one comparison fails, there is no need to evaluate the remainder
as the true-false condition has been met and the test is aborted with
a "false" result.  The only time the right-most comparison (or top level
comparison if nested) is evaluated is if all other comparisons are
true.

----------------------------------------------------------------------
- Rick Stevens, Systems Engineer                       rps2 at nerd.com -
- Hosting Consulting, Inc.                                           -
-                                                                    -
-  Diplomacy: The art of saying "Nice doggy!" until you can find a   -
-                            big enough rock.                        -
----------------------------------------------------------------------




More information about the fedora-list mailing list