Gentoo Archives: gentoo-amd64

From: Richard Freeman <rich0@g.o>
To: gentoo-amd64@l.g.o
Subject: Re: [gentoo-amd64] How to use &&
Date: Sun, 04 Jan 2009 15:57:31
Message-Id: 4960DC68.1060604@gentoo.org
In Reply to: Re: [gentoo-amd64] How to use && by Maciej Kazulak
1 Maciej Kazulak wrote:
2 > That's how bash evaluates logic expressions. There's even a fancy word
3 > for it i think. Point is it will execute commands only to the point
4 > where it can determine the logic result.
5
6 The fancy word for it would probably be short circuit evaluation:
7
8 http://en.wikipedia.org/wiki/Short-circuit_evaluation
9
10 When placing code in a boolean expression it is critical to understand
11 whether the boolean operator will be evaluated in a short-circuit
12 fashion in any language.
13
14 I'm not aware of any language that works this way, but potentially you
15 could even have out-of-order boolean evaluation (ie echo "hello" &&
16 false does nothing) in a very well optimized language.
17
18 Using && in this way is technically a bit of a hack - the && operator
19 was intended to evaluate a boolean expression and not as a replacement
20 for "if". That doesn't stop 99.9999999% of shell scripters from using
21 it, however. It is less of a hack than writing the equivalent of 30
22 lines of code inside a for(<gobs of code> ; <gobs of code> ; <gobs of
23 code>); statement in C. :)

Replies

Subject Author
Re: [gentoo-amd64] How to use && Barry Schwartz <chemoelectric@×××××××××××××.org>
[gentoo-amd64] Re: How to use && Duncan <1i5t5.duncan@×××.net>