From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org
Subject: Re: Belaboring substitution syntax
Date: Tue, 15 Nov 2022 17:49:50 -0800 [thread overview]
Message-ID: <CAH+w=7ZWz0cdQLc9kr1cTBun=5TW4=1meSzc8Uf4JBTM9Wa9NQ@mail.gmail.com> (raw)
In-Reply-To: <879a8fb0-74b7-5250-d2ff-2f60585f48a4@eastlink.ca>
On Tue, Nov 15, 2022 at 6:18 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> I sorta get that '[@]' is saying: "process this array line by line" thus preserving array indexes. I'm not sure about the quotes.
This follows the same rules as $@. If you write "${array[@]}" with
the quotes, you get each element of the array separately quoted. This
often doesn't matter in zsh but always matters in other shells or when
SH_WORD_SPLIT is enabled in zsh.
BTW you need to stop thinking of these as "lines" -- line breaks have
nothing to do with it.
Conversely if you write "${array}" or ${array[*]}" either one with the
quotes, you get the entire array joined into a single string.
> and sometimes the quotes seem to protect stuff from expansion or interpretation:
Quotes ALWAYS protect SOMETHING from expansion or interpretation,
unless you start throwing other things in there such as eval. WHAT is
protected differs: Double quotes allow ${param} and $(process) and
$((math)) replacements but protect syntax tokens and glob patterns and
whitespace; single quotes protect pretty much everything.
> In my case, leaving the quotes out doesn't *seem* to matter but I know it would bite me eventually. When would it bite me?
In the specific example in this thread, zsh's default array behavior
(no_SH_WORD_SPLIT) is sufficient, but if you later enclose that or a
similar construct in a deeper context, yes, you might be bitten.
> Do we not have an operator that says (in English): If string A is a substring of B, then return B, else return nothing.
Your trouble is the definition of "nothing." ${B:#*A*} does that, but
in your example here, you don't actually want "nothing", you want "the
empty string" which is still something.
In fact the example in question is "if A is NOT a substring of B then
return B, else return the empty string."
So we have this:
Do a replacement on every element of $cc is ${cc[@]/...}
Replacement must start at beginning of element ${cc[@]/#...}
Replacement must also end at end of element ${cc[@]/#%...}
Use $zsh_case as a pattern is $~zsh_case
Match that pattern anywhere with $filter is *$~zsh_case${filter}*
Match NOT that whole pattern is ^*$~zsh_case${filter}*
Put it all together: ${cc[@]/#%^*$~zsh_case${filter}*
Replace all that with the empty string is just close the brace
${cc[@]/#%^*$~zsh_case${filter}*}
... and then the double-quotes for safety.
About the only thing your magical operator could remove is putting
*...* around the pattern to mean "is a substring".
next prev parent reply other threads:[~2022-11-16 1:50 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-10 0:43 coloring substitution seems to eat next line Ray Andrews
2022-11-10 8:02 ` Roman Perepelitsa
2022-11-10 18:25 ` Ray Andrews
2022-11-10 18:36 ` Roman Perepelitsa
2022-11-10 18:45 ` Ray Andrews
2022-11-10 18:54 ` Bart Schaefer
2022-11-10 19:28 ` Ray Andrews
2022-11-10 20:22 ` Bart Schaefer
2022-11-10 21:42 ` Ray Andrews
2022-11-10 21:51 ` Roman Perepelitsa
2022-11-11 17:24 ` Ray Andrews
2022-11-10 22:47 ` Bart Schaefer
2022-11-10 23:07 ` Ray Andrews
2022-11-10 23:27 ` Bart Schaefer
2022-11-11 15:00 ` Ray Andrews
2022-11-11 18:15 ` Bart Schaefer
2022-11-11 18:50 ` Ray Andrews
2022-11-11 19:25 ` Bart Schaefer
2022-11-11 21:26 ` Ray Andrews
2022-11-12 4:24 ` Bart Schaefer
2022-11-12 14:03 ` Ray Andrews
2022-11-13 15:09 ` Ray Andrews
2022-11-14 14:12 ` Roman Perepelitsa
2022-11-14 17:08 ` Ray Andrews
2022-11-14 17:12 ` Roman Perepelitsa
2022-11-14 18:58 ` Ray Andrews
2022-11-14 20:00 ` Bart Schaefer
2022-11-14 23:25 ` Ray Andrews
2022-11-15 14:17 ` Belaboring substitution syntax Ray Andrews
2022-11-16 1:49 ` Bart Schaefer [this message]
2022-11-16 2:54 ` Ray Andrews
2022-11-16 6:26 ` Bart Schaefer
2022-11-16 14:08 ` Ray Andrews
2022-11-16 14:13 ` Roman Perepelitsa
2022-11-17 2:31 ` Bart Schaefer
2022-11-17 8:59 ` Roman Perepelitsa
2022-11-17 16:02 ` Ray Andrews
2022-11-16 20:46 ` Ray Andrews
2022-11-16 10:32 ` Roman Perepelitsa
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=7ZWz0cdQLc9kr1cTBun=5TW4=1meSzc8Uf4JBTM9Wa9NQ@mail.gmail.com' \
--to=schaefer@brasslantern.com \
--cc=rayandrews@eastlink.ca \
--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).