1

In zsh, if I run:

a=$(echo "foo, bar")
echo ${a[(ws:, :)1]}

I get foo as you would expect (w causes the index to refer to words s:, : makes , be the word separator).

However, if I try to combine these:

echo ${$(echo "foo, bar")[(ws:, :)1]}

I get foo,. For some reason the w flag is working correctly, but the s:, : flag is completely ignored.

What am I doing wrong here?

More info: This is just a problem with $() inside ${}. If I nest ${} inside ${} there is no such problem.

$ zsh --version
zsh 5.2 (x86_64-unknown-linux-gnu)
RPichioli
  • 3,245
  • 2
  • 25
  • 29
Radvendii
  • 41
  • 3

1 Answers1

0

Field splitting is done before the index operation kicks in, so the behaviour you see is what I would expect. You can get the expected result by

echo ${"$(echo "foo, bar")"[(ws:, :)1]}

One note aside: My first attempt was to rewrite your expression using the ?{==...} form of parameter substitution, which is supposed to inhibit field splitting, i.e.

echo ${${==$(echo "foo, bar")}[(ws:, :)1]}

and this did not work. I have no idea why...

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • 1
    `${==` only unsets the sh_word_split option for the expansion. In effect, that is only asserting a default and is unlikely to have much effect. – okapi Oct 06 '16 at 17:52
  • Can you explain a little more what you mean by "field splitting" and "index operation", and why your solution works? It seems like quoting "$(echo " and ")" would just give a syntax error. – Radvendii Oct 07 '16 at 18:57
  • Could it be that you are using an older Zsh version? You can leave out the inner quotes or replace them by single quotes. See [this screenshot](https://postimg.org/image/47xhi0zwz/). – user1934428 Oct 09 '16 at 10:49
  • oh. I see what's happening with tthe quoting now. I still don't understand what is meant by "field splitting" – Radvendii Oct 10 '16 at 04:26