Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Word splitting/joining inside [[ ]]
- X-seq: zsh-workers 37391
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Word splitting/joining inside [[ ]]
- Date: Fri, 11 Dec 2015 20:31:47 -0800
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:to:subject:mime-version:content-type; bh=fTR0+YSoAqBaFU+LsI7VWnqkWfBQ9GrIbHuWOOLEwZE=; b=fXc1emUKAJ4sIXXJT5gvDTs4TyGKxGiHCvdS7rQpzRvgDeSBN1ifrrtle85f5K2ljY 3ZCSaJT7S4OzJvJ4+PABtm5swDDRsV3QH8h6LzQU3O5ldwrOnqKb+RrdiZsaHPdwA5f/ CsDPPnKGbE6KVKk/NxepO7U8BT9zbYR2i35cUi/ySuI7+il09I6wiD6RVPHuxCo5TZuI 3QaPhzRUO9MH5BumNt142IBhuqzN7fhPPonGZIaBRt/j7pmipy7wHpf6YMJoX6MLScxE y3FCG9dctfBT65DnegT56tJ/KaB7h9/YLpAsMx7rQhR29jNJTSFjTpoa/F7US2IKu71H Lc4g==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
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?
Messages sorted by:
Reverse Date,
Date,
Thread,
Author