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)
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, ie
echo ${${==$(echo "foo, bar")}[(ws:, :)1]}
and this did not work. I have no idea why...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.