From owner-freebsd-chat@FreeBSD.ORG Fri Jul 17 23:18:18 2009 Return-Path: Delivered-To: chat@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3D62E106566C for ; Fri, 17 Jul 2009 23:18:18 +0000 (UTC) (envelope-from brett@lariat.net) Received: from lariat.net (lariat.net [66.119.58.2]) by mx1.freebsd.org (Postfix) with ESMTP id CD4408FC1B for ; Fri, 17 Jul 2009 23:18:17 +0000 (UTC) (envelope-from brett@lariat.net) Received: from anne-o1dpaayth1.lariat.net (IDENT:ppp1000.lariat.net@lariat.net [66.119.58.2]) by lariat.net (8.9.3/8.9.3) with ESMTP id QAA15292 for ; Fri, 17 Jul 2009 16:57:16 -0600 (MDT) Message-Id: <200907172257.QAA15292@lariat.net> X-Mailer: QUALCOMM Windows Eudora Version 7.1.0.9 Date: Fri, 17 Jul 2009 16:57:12 -0600 To: chat@freebsd.org From: Brett Glass Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Cc: Subject: Bourne shell short-circuit operators improperly documented X-BeenThere: freebsd-chat@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Non technical items related to the community List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Jul 2009 23:18:18 -0000 Everyone: I'm teaching some new employees UNIX basics, and just ran into the following text on the sh(1) man page: Short-Circuit List Operators ``&&'' and ``||'' are AND-OR list operators. ``&&'' executes the first command, and then executes the second command if the exit status of the first command is zero. ``||'' is similar, but executes the second com- mand if the exit status of the first command is nonzero. ``&&'' and ``||'' both have the same priority. This is exactly backward. && is a "short circuit AND." It stops right away and doesn't evaluate its second operand if its first operand is 0. Why? Because if one operand of an AND operation is 0, we already know the result: 0. It can't be otherwise. Likewise, || is a "short circuit OR." It stops right away and doesn't evaluate its second operand if the first operand is 1 (or anything nonzero). Why? Because if one operand of an OR operation is nonzero, the result can never be 0. How could this error have persisted in the FreeBSD documentation for so long? --Brett Glass