From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh <zsh-users@zsh.org>
Subject: Re: Mix and match parameter expansion flags and sub-scripting flags + quoting
Date: Fri, 29 Dec 2017 10:43:30 -0800 [thread overview]
Message-ID: <CAH+w=7bC2D+n8t-8UwD0Q++4mg98JvsJpYMmU0iC=yqr8ozWOQ@mail.gmail.com> (raw)
In-Reply-To: <CA+rB6GLSgUkWiBRMmNQ_gc-A2pABpPJVnR8xf9tpYaK3e8JMyQ@mail.gmail.com>
On Fri, Dec 29, 2017 at 7:29 AM, Jim <linux.tech.guy@gmail.com> wrote:
> Hello,
>
> The intent is, using only "native zsh" tools, return the 'shell'
> as set in /etc/passwd for my own $USER without using any external
> utilities, gnu or otherwise. As in UNIX/Linux, zsh also has more then
> one way of doing the same thing. So as a training exercise I decided
> to use as many variants as I could to do the same thing. In the process
> I found that what I though would work, did not return the intended
> result.
I haven't gone through your cases in any detail, but there are almost
certainly three things causing your problems:
(1) Nested expansions plus quoting cause an array in the inner
expansion to be joined into a string in most cases, unless you use
special syntax to preserve the array
(2) Single-element arrays behave like scalars (strings) in several cases
(3) Subscript syntax applies to scalars, to extract substrings
Those last two put together can cause a lot of head-scratching.
Recent zsh extended ${(A)array} to mean "assure $array continues to be
treated as an array, even when it would otherwise become a string"
specifically to avoid this issue. Some of your examples work around
it by using the (w) subscript flag, but that isn't quite the same,
because it still starts from a string and re-splits the string before
indexing. In older zsh for most cases you can preserve array-ness by
using the (@) flag, there are just a few [item (2) above] where that
didn't work which caused us to provide (A).
Compare:
% x=("abc def")
% print ${x[1]}
abc def
% print ${${x}[1]}
abc def
% print "${${x}[1]}"
a
% print ${"${x}"[1]}
a
% print ${"${x}"[(w)1]}
abc
% print ${"${(A)x}"[1]}
abc def
% print "${${(@)x}[1]}"
abc def
Figuring out at what level of a nested expansion you need to add (A)
or (@) is the other part of the equation. Sometimes you need to add
an extra level of nesting just to force (A) to be applied to the
correct value.
> My distributions currently distributes ZSH_VERSION 5.3.1.
That should be new enough for (A) to work.
next prev parent reply other threads:[~2017-12-29 18:43 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-29 15:29 Jim
2017-12-29 18:43 ` Bart Schaefer [this message]
2017-12-31 19:01 ` Jim
2018-01-01 8:27 ` dana
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAH+w=7bC2D+n8t-8UwD0Q++4mg98JvsJpYMmU0iC=yqr8ozWOQ@mail.gmail.com' \
--to=schaefer@brasslantern.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).