zsh-workers
 help / color / mirror / code / Atom feed
* Possible bug with $~pattern, (#mi)
@ 2015-10-07 15:28 Sebastian Gniazdowski
  2015-10-07 15:39 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2015-10-07 15:28 UTC (permalink / raw)
  To: zsh-workers

Hello,
first one and third one are the same, but only first invocation works.
Not sure if second one should work:

 # a="1234"; beg="#" echo ${a/$~beg(#mi)1/-}
-234
 # a="1234"; beg="#"; num=1; echo ${a/$~beg(#mi)$~num/-}
zsh: bad pattern: #(#mi)1
 # a="1234"; beg="#" echo ${a/$~beg(#mi)1/-}
zsh: bad pattern: #(#mi)1
 #


Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Possible bug with $~pattern, (#mi)
  2015-10-07 15:28 Possible bug with $~pattern, (#mi) Sebastian Gniazdowski
@ 2015-10-07 15:39 ` Peter Stephenson
  2015-10-07 16:03   ` Bart Schaefer
  2015-10-07 16:06   ` Sebastian Gniazdowski
  0 siblings, 2 replies; 5+ messages in thread
From: Peter Stephenson @ 2015-10-07 15:39 UTC (permalink / raw)
  To: zsh-workers

On Wed, 7 Oct 2015 17:28:04 +0200
Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> first one and third one are the same, but only first invocation works.
> Not sure if second one should work:
> 
>  # a="1234"; beg="#" echo ${a/$~beg(#mi)1/-}
> -234
>  # a="1234"; beg="#"; num=1; echo ${a/$~beg(#mi)$~num/-}
> zsh: bad pattern: #(#mi)1
>  # a="1234"; beg="#" echo ${a/$~beg(#mi)1/-}
> zsh: bad pattern: #(#mi)1
>  #

The difference is the first one doesn't have $beg defined at the point
where the expansion takes place, because beg is being put into the
environment to use when the command is run.  The environment of the
command is not the same thing as the set of variables avaiable to the
shell for preparing the command line.

So what you're executing that works is actually

a="1234"
echo ${a/(#mi)1/-}

After that, the result is self explanatory --- #(#mi) is a bad pattern
(with EXTENDED_GLOB set).

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Possible bug with $~pattern, (#mi)
  2015-10-07 15:39 ` Peter Stephenson
@ 2015-10-07 16:03   ` Bart Schaefer
  2015-10-07 16:06   ` Sebastian Gniazdowski
  1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2015-10-07 16:03 UTC (permalink / raw)
  To: zsh-workers

On Oct 7,  4:39pm, Peter Stephenson wrote:
} Subject: Re: Possible bug with $~pattern, (#mi)
}
} On Wed, 7 Oct 2015 17:28:04 +0200
} Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
} > first one and third one are the same, but only first invocation works.
} > Not sure if second one should work:
} > 
} >  # a="1234"; beg="#" echo ${a/$~beg(#mi)1/-}
} > -234
} >  # a="1234"; beg="#"; num=1; echo ${a/$~beg(#mi)$~num/-}
} > zsh: bad pattern: #(#mi)1
} 
} The difference is the first one doesn't have $beg defined at the point
} where the expansion takes place

OK, but what I think he's trying to achieve is

    % a="1234"
    % b="4321"
    % print ${a/#(#mi)1/-} ${b/#(#mi)1/-}
    -234 4321

which works and treats the first "#" as part of the ${var//} expansion
rather than as part of the pattern.

So the complete answer is that there is no way to dynamically insert
the beginning marker # or end marker % in ${a/#p/r} or ${a/%p/r} --
they must appear literally, otherwise they become part of the pattern.

If you want to do this dynamically, you have to use the extendedglob
anchoring syntax rather than the parameter expansion anchoring syntax:

    % beg='(#s)'
    % print ${a/$~beg(#mi)1/-}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Possible bug with $~pattern, (#mi)
  2015-10-07 15:39 ` Peter Stephenson
  2015-10-07 16:03   ` Bart Schaefer
@ 2015-10-07 16:06   ` Sebastian Gniazdowski
  2015-10-07 16:14     ` Peter Stephenson
  1 sibling, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2015-10-07 16:06 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

True, beg wasn't set in first invocation. However pattern #(#mi)1 is correct:

# a="1234"; echo ${a/#(#mi)1/-}
-234

When beg is used (this time correctly):

# a="1234"; beg="#"; echo ${a/$~beg(#mi)1/-}
zsh: bad pattern: #(#mi)1

Best regards,
Sebastian Gniazdowski


On 7 October 2015 at 17:39, Peter Stephenson <p.stephenson@samsung.com> wrote:
> On Wed, 7 Oct 2015 17:28:04 +0200
> Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
>> first one and third one are the same, but only first invocation works.
>> Not sure if second one should work:
>>
>>  # a="1234"; beg="#" echo ${a/$~beg(#mi)1/-}
>> -234
>>  # a="1234"; beg="#"; num=1; echo ${a/$~beg(#mi)$~num/-}
>> zsh: bad pattern: #(#mi)1
>>  # a="1234"; beg="#" echo ${a/$~beg(#mi)1/-}
>> zsh: bad pattern: #(#mi)1
>>  #
>
> The difference is the first one doesn't have $beg defined at the point
> where the expansion takes place, because beg is being put into the
> environment to use when the command is run.  The environment of the
> command is not the same thing as the set of variables avaiable to the
> shell for preparing the command line.
>
> So what you're executing that works is actually
>
> a="1234"
> echo ${a/(#mi)1/-}
>
> After that, the result is self explanatory --- #(#mi) is a bad pattern
> (with EXTENDED_GLOB set).
>
> pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Possible bug with $~pattern, (#mi)
  2015-10-07 16:06   ` Sebastian Gniazdowski
@ 2015-10-07 16:14     ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2015-10-07 16:14 UTC (permalink / raw)
  To: zsh-workers

On Wed, 7 Oct 2015 18:06:43 +0200
Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> When beg is used (this time correctly):
> 
> # a="1234"; beg="#"; echo ${a/$~beg(#mi)1/-}
> zsh: bad pattern: #(#mi)1

Bart's explained this in a more practical way, but you might want to
read the documentation.  It's not well written, actually: the point it's
making is that the # or % isn't part of the pattern, it's part of the
substitution syntax.  "The pattern may be preceded by..." or "The / may
be followed by..." would put it better, so I might change it.  "%" isn't
part of pattern syntax at all, so that would fail silently.

   The pattern may begin with a `#', in which case the pattern must
   match at the start of the string, or `%', in which case it  must
   match  at  the end of the string, or `#%' in which case the pat‐
   tern must match the entire string.  The repl  may  be  an  empty
   string,  in  which  case  the final `/' may also be omitted.  To
   quote the final `/' in other cases it should be  preceded  by  a
   single backslash; this is not necessary if the `/' occurs inside
   a substituted parameter.  Note also that the `#',  `%'  and  `#%
   are  not  active  if  they occur inside a substituted parameter,
   even at the start.

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-10-07 16:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-07 15:28 Possible bug with $~pattern, (#mi) Sebastian Gniazdowski
2015-10-07 15:39 ` Peter Stephenson
2015-10-07 16:03   ` Bart Schaefer
2015-10-07 16:06   ` Sebastian Gniazdowski
2015-10-07 16:14     ` Peter Stephenson

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).