zsh-workers
 help / color / mirror / code / Atom feed
* Word splitting/joining inside [[ ]]
@ 2015-12-12  4:31 Bart Schaefer
  2015-12-13 18:28 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Bart Schaefer @ 2015-12-12  4:31 UTC (permalink / raw)
  To: zsh-workers

I have a string of words:

words="I have a string of words"

What I want to learn is whether the words are already in sorted order.

So my thought is to split the string on spaces, sort the resulting array,
and join the string back together again on spaces, and compare that string
to the original string.

So I wrote this:

[[ $words = ${(on)=words} ]]

Doesn't work.  It's always true.  Confused, I setopt xtrace and discover:

torch% [[ $words != ${(on)=words} ]]
+zsh:6> [[ 'I have a string of words' = I\ have\ a\ string\ of\ words ]]

Oh, I have the (on) at the wrong nesting level.

torch% [[ $words = ${(on)${=words}} ]]
+zsh:7> [[ 'I have a string of words' == I\ have\ a\ string\ of\ words ]]

Er, apparently that's not it.  Works fine outside [[ ]]:

torch% print ${(on)${=words}}
+zsh:8> print a have I of string words
a have I of string words

But then so does the original:

torch% print ${(on)=words} 
+zsh:9> print a have I of string words
a have I of string words

By accident I discovered that adding a supposedly-meaningless extra level
of nested expansion makes it work:

torch% [[ $words = ${${(on)=words}} ]]
+zsh:12> [[ 'I have a string of words' == a\ have\ I\ of\ string\ words ]]

What's going on here?


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

end of thread, other threads:[~2015-12-13 18:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-12  4:31 Word splitting/joining inside [[ ]] Bart Schaefer
2015-12-13 18:28 ` 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).