zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: &&||
Date: Mon, 19 Feb 2018 21:57:26 +0000	[thread overview]
Message-ID: <20180219215726.4c25cc7d@ntlworld.com> (raw)
In-Reply-To: <64c5472a-b174-00b6-7ab0-b65d664be675@eastlink.ca>

On Mon, 19 Feb 2018 13:12:20 -0800
Ray Andrews <rayandrews@eastlink.ca> wrote:

> function test ()
> {
>      _aaray=`ls`

You never need to use output from ls in a shell unless you need long
output like ls -l (and there are ways round that, too).  This should be

_aarray=(*)

and you get the full power of globbing if you wanted to vary the
contents in some way.  Strictly, I suppose it should be

_aarray=(*(N))

to give you an empty array rather than an error if there are no files
that don't begin with a dot.

>      [ true ] && { print -l "${ _aaray[@]}" | GREP_COLOR='01;33' egrep 

Do you mean just "true"?  Not sure what putting this in square brackets
is supposed to achieve.  It does work, but purely by virtue of the fact
that the string "true" has non-zero length --- as does the string
"false", which is one of several reasons I'd avoid this.

> --color=always "$1"\
>
> | GREP_COLOR='01;31' egrep --color=always "$2" }\
> 
> || echo bust!

If you read the documentation about && and || (OK, OK, I'm joking, I'm
joking) you'll find the statement:

  Both operators have equal precedence and are left associative. 

That's written for geeks, but what it means is the shell simply looks
through from left to right, taking the &&s and ||s as they come:

- If it finds "&&" at any point, it executes the following chunk if the
previous returned status true.  Otherwise, it skips it and looks for any
further "&&" or "||".

- If it finds "||" at any point, it executes the following chunk if the
previous returned status false.  Otherwise, it skips it and looks for
any further "&&" or "||".

I've skipped over the fact that actually it may not have executed
the chunk before the "&&" or "||" it's looking at.  So where I
said "the previous status returned" you should really think of
"the last status of anything it did execute was...".  So

false && true || true

- runs false to get status (surprise!) false.

- looks at the "&&" and decides "nah, skip the next bit".

- looks at the "||" and still has status false, so executes the true.

That should give you enough of a hint to follow any combination.

Note this is not how && and || work in C, or even in zsh's own
arithmetic context which is much more like C than the standard shell
syntax is.

You can affect the precedence with braces, but they need to surround the
"&&" or "||" expression you want to protect.  In your case you've simply
surrounded a pipeline which would be run in one go anyway:

% echo one && echo one | sed -e s/o/a/
one
ane

is just the same as

% echo one && { echo one | sed -e s/o/a/ }
one
ane

and sticking more of the poor suffering &&s and ||s after doesn't change
it, either.

> }

pws


  reply	other threads:[~2018-02-19 22:07 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-19 21:12 &&|| Ray Andrews
2018-02-19 21:57 ` Peter Stephenson [this message]
2018-02-19 22:47   ` &&|| Ray Andrews
2018-02-20  9:26     ` &&|| Peter Stephenson
2018-02-20  7:54       ` &&|| Ray Andrews
2018-02-20 17:07         ` &&|| Peter Stephenson
2018-02-20 19:24           ` &&|| Ray Andrews
2018-02-20 20:28             ` &&|| Bart Schaefer
2018-02-20 21:45               ` &&|| Ray Andrews
  -- strict thread matches above, loose matches on Subject: below --
2004-11-16 13:45 !!:$ keef
2004-11-16 14:22 ` !!:$ Peter Stephenson
2004-11-16 14:31   ` !!:$ Stephane Chazelas
2004-11-16 15:37   ` !!:$ Bart Schaefer
2004-11-16 16:08     ` !!:$ Peter Stephenson
2004-11-17  0:03 ` !!:$ Bart Schaefer
2004-11-17 19:25   ` !!:$ Danek Duvall
2004-11-18  1:10     ` !!:$ Bart Schaefer
2003-07-02 15:06 ?????? JEFF BICKLEY
2003-07-02 19:56 ` ?????? Thorsten Haude

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180219215726.4c25cc7d@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).