Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

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

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


Messages sorted by: Reverse Date, Date, Thread, Author