Skip to main content
Formatting
Source Link
jasonwryan
  • 74.9k
  • 35
  • 204
  • 230

There is a notion of "short cutting".
When

When (expr1 && expr2)(expr1 && expr2) is evaluated - expr2expr2 is only evaluated if exp1exp1 evaluates to "true". This is because both expr1expr1 AND expr2expr2 have to be true for (expr1 && expr2)(expr1 && expr2) to be true. If expr1expr1 evaluates to "false" expr2expr2 is NOT evalued (short cut) because (expr1 && expr2)(expr1 && expr2) is already "flase".
 

Try the following - Assumeassume file F1F1 exists & file F2F2 does not exist:
( [ -s F1 ] && echo "File Exists" ) (will print "File Exists" - no short cut)
( [ -s F2 ] && echo "File Exists" ) (will NOT print "File Exists" - short cut)

( [ -s F1 ] && echo "File Exists" ) # will print "File Exists" - no short cut ( [ -s F2 ] && echo "File Exists" ) # will NOT print "File Exists" - short cut

Similarly for |||| (or) - but short cutting is reversed.

There is a notion of "short cutting".
When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".
 

Try the following - Assume file F1 exists & file F2 does not exist:
( [ -s F1 ] && echo "File Exists" ) (will print "File Exists" - no short cut)
( [ -s F2 ] && echo "File Exists" ) (will NOT print "File Exists" - short cut)

Similarly for || (or) - but short cutting is reversed.

There is a notion of "short cutting".

When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".

Try the following - assume file F1 exists & file F2 does not exist:

( [ -s F1 ] && echo "File Exists" ) # will print "File Exists" - no short cut ( [ -s F2 ] && echo "File Exists" ) # will NOT print "File Exists" - short cut

Similarly for || (or) - but short cutting is reversed.

Source Link
Larry
  • 41
  • 1

There is a notion of "short cutting".
When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".

Try the following - Assume file F1 exists & file F2 does not exist:
( [ -s F1 ] && echo "File Exists" ) (will print "File Exists" - no short cut)
( [ -s F2 ] && echo "File Exists" ) (will NOT print "File Exists" - short cut)

Similarly for || (or) - but short cutting is reversed.