zsh-users
 help / color / Atom feed
* zsh parameter expansion replacement pattern parses string differently if its input is a variable instead of inlined
@ 2020-03-10 20:04 ` Ross Goldberg
  2020-03-11 10:13   ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Ross Goldberg @ 2020-03-10 20:04 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1061 bytes --]

A zsh parameter expansion replacement pattern parses string differently if
its input is a variable instead of inlined.

I assume that it has something to do with character escaping, but am not
sure how to work around it.

In the code below, >, * & 2.5.8 should be filtered out of the printf
output, which correctly occurs in the first parameter expansion using the
variable named versions.

But when the curl call is inlined, then the above 3 values are not filtered
out.

How can I inline the curl call yet still filter out the 3 values?

#!/usr/bin/env zsh

setopt EXTENDED_GLOB

# variable: 3 values are correctly filtered out
versions=$(curl '--silent' '--location' '
https://api.sdkman.io/2/candidates/groovy/darwin/versions/list?current=2.5.8&installed=2.5.8
')
printf -- '%s\n' ${${(Z+n+)versions//[*+>][ *+>]# [[:graph:]]##}}

# inlined: 3 values are not filtered out
printf -- '%s\n' ${${(Z+n+)$(curl '--silent' '--location' '
https://api.sdkman.io/2/candidates/groovy/darwin/versions/list?current=2.5.8&installed=2.5.8')//[*+>][
*+>]# [[:graph:]]##}}

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

* Re: zsh parameter expansion replacement pattern parses string differently if its input is a variable instead of inlined
  2020-03-10 20:04 ` zsh parameter expansion replacement pattern parses string differently if its input is a variable instead of inlined Ross Goldberg
@ 2020-03-11 10:13   ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2020-03-11 10:13 UTC (permalink / raw)
  To: zsh-users

On Tue, 2020-03-10 at 16:04 -0400, Ross Goldberg wrote:
> How can I inline the curl call yet still filter out the 3 values?
> 
> #!/usr/bin/env zsh
> 
> setopt EXTENDED_GLOB
> 
> # variable: 3 values are correctly filtered out
> versions=$(curl '--silent' '--location' '
> https://protect2.fireeye.com/url?k=1c92524f-415c53fb-1c93d900-000babdfecba-907de8ae2883278c&u=https://api.sdkman.io/2/candidates/groovy/darwin/versions/list?current=2.5.8&installed=2.5.8
> ')
> printf -- '%s\n' ${${(Z+n+)versions//[*+>][ *+>]# [[:graph:]]##}}
> 
> # inlined: 3 values are not filtered out
> printf -- '%s\n' ${${(Z+n+)$(curl '--silent' '--location' '
> https://protect2.fireeye.com/url?k=7fed1228-2223139c-7fec9967-000babdfecba-dd90873f5cc32e48&u=https://api.sdkman.io/2/candidates/groovy/darwin/versions/list?current=2.5.8&installed=2.5.8')//[*+>][
> *+>]# [[:graph:]]##}}

If I'm following correctly (a simpler example would probably help if I'm
missing the point, though I think what I've done gives what you want),
you need to put double quotes around the "$(curl ...)" part.


printf -- '%s\n' ${${(Z+n+)"$(curl '--silent' '--location' 'https://protect2.fireeye.com/url?k=7fed1228-2223139c-7fec9967-000babdfecba-dd90873f5cc32e48&u=https://api.sdkman.io/2/candidates/groovy/darwin/versions/list?current=2.5.8&installed=2.5.8')"//[*+>][ *+>]# [[:graph:]]##}}


The big difference is that the assignment in the other version is
already turning the value into a single word, whereas in the second case
you will get the output split into a set of words; the substitution then
operates on those words separately, which isn't what you want.

pws


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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20200310200552eucas1p283e8a92d686e34198f78b647370f5a24@eucas1p2.samsung.com>
2020-03-10 20:04 ` zsh parameter expansion replacement pattern parses string differently if its input is a variable instead of inlined Ross Goldberg
2020-03-11 10:13   ` Peter Stephenson

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git