zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: difference between ~ & ^ negation
Date: Wed, 01 Jan 2014 13:44:59 -0800	[thread overview]
Message-ID: <140101134459.ZM8931@torch.brasslantern.com> (raw)
In-Reply-To: <XnsA2A89D5B3B275davidrayninfocouk@80.91.229.13>

On Jan 1,  3:28pm, zzapper wrote:
>
> What are the advantages/differences between the two types of "ignore" ^ ~

The ^ operator is similar to *~ except that it has high precedence, so
the pattern following ^ (up to a slash) is excluded from the set of all
files in the same level of directory hierarchy; e.g. foo/^bar/baz looks
for baz in any subdirectory of foo except the subdirectory named bar.
Therefore the pattern may not contain a slash.

The ~ operator is very low precedence, so the entire directory search
(including descent into subdirectories) to the left of the tilde is
performed, and the pattern to the right of the tilde is tested against
each file or directory to exclude it from the final result.  This means
that the pattern to the right of the tilde is not actually a glob; a
wildcard in that pattern can match a slash in the tested name, and the
pattern may contain slashes.

Thus X~Z is nearly equivalent to X(e:'[[ $REPLY == Z ]] && reply=():').

To see advantages and disadvantages, consider two of your examples:

> ls -lt **/*~*vssver.scc(.om[1,20])  # excluding vssver.scc 
> ls -lt **/^vssver.scc(.om[1,20])    #  excluding vssver.scc (simpler) 

As you pointed out, the second case is simpler.  However, these are not
quite equivalent; the first one should be:

    ls -lt **/*~*/vssver.scc(.om[1,20])

Otherwise you also ignore "OLDvssver.scc" (for example), so the second
case is also easier to use correctly.  I find the most difficult thing
to remember about ~ is that actual globbing happens only on the left,
so the right side has to match the path names as simple strings.

On the other hand, suppose you want to ignore vssver.scc only in the
myproject subdirectory, but include it everywhere else:

    ls -lt **/*~*/myproject/vssver.scc

There's no way to express this with the ^ operator except by using two
globs and a qualifier:

    ls -lt **/^vssver.scc **/^myproject/vssver.scc(N)

(The qualifier avoids a "no match" error if there is no vssver.scc file
anywhere outside of myproject.)

Now suppose you want everything that's neither in myproject nor named
vssver.scc; these should all be equivalent:

    ls -lt **/^myproject/^vssver.scc
    ls -lt **/*~*/(myproject/*|vssver.scc)
    ls -lt **/^vssver.scc~*/myproject/*

But these next two are not, do you see why?

    ls -lt **/^myproject~*/vssver.scc
    ls -lt **/^myproject/*~*/vssver.scc

Important note: ^ is special only in cases where / is special (globs).


  reply	other threads:[~2014-01-01 21:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-01 15:28 zzapper
2014-01-01 21:44 ` Bart Schaefer [this message]
2014-01-02 21:01   ` Peter Stephenson
2014-01-02 21:36     ` Bart Schaefer
2014-01-02 22:14       ` Peter Stephenson
2014-01-03  1:55       ` Mikael Magnusson
2014-01-03  7:37         ` Bart Schaefer
2014-01-03 19:48           ` Peter Stephenson
2014-01-03 20:10             ` Peter Stephenson
2014-01-03 21:43               ` Bart Schaefer
2014-01-04 21:11                 ` zzapper
2014-01-05 10:01                   ` Eike von Seggern
     [not found] ` <8lp61n01B02mHnv01lp8uc>
2014-01-03 20:56   ` how to shut off auto spell steve
2014-01-03 21:52     ` Bart Schaefer
     [not found]     ` <9ZwH1n01Z02mHnv01ZwLWZ>
2014-01-03 22:07       ` steve

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=140101134459.ZM8931@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).