From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 7713 invoked from network); 18 Oct 2022 16:08:42 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 18 Oct 2022 16:08:42 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1666109322; b=HXX1+a47PdOUon0rdFzNY3Asq0xwuXiMBrNPuw82+OQXxv182w4mghcaQzP6ASvjZS2DJYPrmT 2bycVerpf19Nfz8lvGeFO3wWAtlX2JXhB271FQmQfbMOXWqyRn1tNC8TYCveEbWylM5atBhahR pRGl+oiIvhHzssFq8v+qUObvO4UukW1rsz6IZ47I5rXKQ7mBDBiRxdPsfGwKuuaQSFNxogGF/h WOTj4J6FJNaNSJelOFMOtgzNenjlba4nnVn800bpIa3LlWhiQYCTNtq1h6Bump/N+3zk68D2n1 rHzxv+lj7lCkScQYHetiYXHw5Wh8SQIGKUXGNivc9p/MWQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-yb1-f176.google.com) smtp.remote-ip=209.85.219.176; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1666109322; bh=bhjGjFLFMAH111O/p9/BqEjNiUya3NcvfAUeP9aabBU=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=VsRNUM08yPefv6gBzGX4FquoHP2Xd1A6CNKddlNVeoYwyIdbCKo+/zBt7iyWHeBYciMAC/TsxX ZNKFnON7yLYxunj82wU4oAdJQfgzBFh3ECg2gS13Ox0q3SQAs6UCuylmd0XCvI15oIFNYoEPrC vjjObl5zbYkUik5CRspfnnFtIGQSt4Zzeprx7x3fN+mIogFPcekE5tDuTqQm1mrd7BSq4b33NZ 8PA7eiGbrDstbl+j7TPBQgp0RxGi9giG856vfNh9G8qugPFeBNsB1sUrdTHcZfh4cyFDcEIu+9 mBGUi4MhhzpFxjuVtxT+hMs0dsHXJyaD8STg1OKnKHklbw==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=CprAln7lbT+FtjpyHT8H5PhPZo1TCfK30odDVt+j3XU=; b=JTm3SXHNjkOeP9gwjJZZJ4Ykmz BnQcKNIMmJw7JJz9unWTkBgrHgUZg19BJNw7TQ0me2d7Y0Wc7jilnjlYvdT4kwBbLH/lPrl6tX4ls xfEAMJbE6MJQ9CyZXD1q3KtHWT8FRYWqWnkhx2a0p87qR9FC6IXUi2lY6DKs8pOXFor/r9YngUViU VG78JsQ3NzgxYx2sL971IL1ofJ3vWtX4zyaVzb1xfoisPiTLWE8HaKcnPn/ZLwWxoiWT0YpntI37a Y9n3ZxDJQ3U871F60OVZRkAh8rp4qREYCPwkwDgF1z9V8OSW7lO5UEEapbvkoxNG+up7M0pOHOpqG pM4kgEkg==; Received: by zero.zsh.org with local id 1okp8z-000Op4-TW; Tue, 18 Oct 2022 16:08:41 +0000 Authentication-Results: zsh.org; iprev=pass (mail-yb1-f176.google.com) smtp.remote-ip=209.85.219.176; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-yb1-f176.google.com ([209.85.219.176]:44686) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1okp8G-000O8L-NE; Tue, 18 Oct 2022 16:07:57 +0000 Received: by mail-yb1-f176.google.com with SMTP id i127so6779312ybc.11 for ; Tue, 18 Oct 2022 09:07:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=CprAln7lbT+FtjpyHT8H5PhPZo1TCfK30odDVt+j3XU=; b=HOeyyFgLITH1TzRIT+v1J1La15WN5xphVphnM3Zwb88WBN8veuvXF8s9//c9513BZD 7PMZpowhDjh/COzQfODun5vDUlQKXHQbPGgjcL6BnmZQkv4y68/E3upBi0mjyIRGOzaM zoGo1hsViFLfg/QVHF62mnK6B7F8sg9OOT94y/jkk2C2rLBzw3kszPk1AvqRTY/ybnkZ kH0Bl0XEkA7whP9nPmhqE+04HS0/x6fMsJtEF+6f5ckb4qBMWdES3SGPf2xX6rwDnbw9 YPt1visN8jHFs8lof+9k1hZ2FZUfvbMR+6HZtgvCJRmQ/SOFG8sKKq6CBrp9+hhGLUGQ BK5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CprAln7lbT+FtjpyHT8H5PhPZo1TCfK30odDVt+j3XU=; b=maBudYTq8u9tLGSvC/0+ok13LXs+X2L7OiDc+LP9FqHB9iLIlWLnTwFJMzf1HuqWjU bww4GZ/oYlm78t9KVaKrkJPpfcWGbWk0/iB25RFFqem7k/Z4GMAC7cnHM6ERLFVu5Q9d 8UBPSyurz+CFRuMDxGJ8IwKivfEFu9xw2HdSwHtQpW1NtK5WFJh9pJtGikOm+I4BWJ+2 fOD9RuXJ7OkdnVKpt2eilhGYpEvgY8+rxeRv+sscC4ULNvwhS/WAOE9I6+RCk/cbIfRE zN2jyE/Pc7GOuBwLPoYWo2VStAM0EVl03dD7sarN83aGEeKO32WNa07tHG2L5nUjXX4+ hN1w== X-Gm-Message-State: ACrzQf3SbfPB30O1m2JiPh7kloJ3yuzy6NStE6kwho68IjS395qSbSJB qYrw8mZhH+8MKSDM7Iknl4gKE44lHNAyntn1hVR974/VXzI= X-Google-Smtp-Source: AMsMyM4JP1l7uk2te+ieeeOE+naJdGAmVEw02HmLpLPTbXbH3CxFYL8Aggb1nD4LeNDJTP833WEoUA6wbqxFBJS7OCs= X-Received: by 2002:a25:406:0:b0:6c2:5a7e:f842 with SMTP id 6-20020a250406000000b006c25a7ef842mr2819083ybe.63.1666109275429; Tue, 18 Oct 2022 09:07:55 -0700 (PDT) MIME-Version: 1.0 References: <4b6eb0b7-ae82-a81f-6a70-784ffc1d39ff@eastlink.ca> In-Reply-To: <4b6eb0b7-ae82-a81f-6a70-784ffc1d39ff@eastlink.ca> From: Roman Perepelitsa Date: Tue, 18 Oct 2022 18:07:42 +0200 Message-ID: Subject: Re: unintuitive bracketing with return value To: Ray Andrews Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" X-Seq: 28225 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: On Tue, Oct 18, 2022 at 4:47 PM Ray Andrews wrote: > > [...] shouldn't the brackets do the intuitive thing and > force the final '||' to be the alternative to the first '&&'? You seem to be thinking of `X && Y || Z` as a single construct but there is no such thing. There is only `X && Y` and `X || Y`. In any programming language I'm familiar with, these operators are left-associative, short-circuit, and the result is always that of the last evaluated operand. Short-circuiting means that the second operand is evaluated by &&/|| if and only if the first operand is true/false. There are two aspects on which programming languages differ when it comes to these operators. 1. Is the result coerced to bool (0 or 1)? Yes in C (and other statically-typed languages), no in Zsh (and other dynamically-typed languages). 2. Does && have higher precedence than ||? Yes in C (and most languages), no in Zsh (and all shells). Once you know this, it should be clear why your program evaluates the way it does. So, how do you put this in practice? Use `if` when you need `if`. Use `X && Y || Z` only when `if` would be wrong. However, even then it's almost always better to do this: if ! X || ! Y; then Z fi Roman.