zsh-users
 help / color / mirror / code / Atom feed
From: Dominik Vogt <dominik.vogt@gmx.de>
To: zsh-users@zsh.org
Subject: Re: Modifiers and parameter expansion?
Date: Fri, 2 Dec 2022 01:52:02 +0100	[thread overview]
Message-ID: <Y4lMMptVvkj62FF3@localhost> (raw)
In-Reply-To: <CAH+w=7b5rQNkfQcRb1zVDbD5CMn-8F67pGS9JvshqyRiFF6nXw@mail.gmail.com>

On Thu, Dec 01, 2022 at 03:42:27PM -0800, Bart Schaefer wrote:
> On Thu, Dec 1, 2022 at 2:39 PM Dominik Vogt <dominik.vogt@gmx.de> wrote:
> >
> > I cannot find anything in the man page that suggest it would work
> > this way.  All occurences of modifiers used with parameters put
> > them in braces.  The rule from the man page:
> >
> >        7. Modifiers
>
> That rule is just telling you at what point during all possible
> expansions the effects of modifiers come into play, it's unrelated to
> the syntax.

Okay.  Why are "%" "%%" "#" "##" "/" "//" refered to in this
section?  They're not called "modifiers" anywhere else.  Maybe the
rule should be named "Pattern replacements and modifiers"?

> In the Parameter Expansion section
>
> ${NAME}
>      The value, if any, of the parameter NAME is substituted.  The
>      braces are required if the expansion is to be followed by a letter,
>      digit, or underscore that is not to be interpreted as part of NAME.
>      In addition, more complicated forms of substitution usually require
>      the braces to be present; exceptions, which only apply if the
>      option KSH_ARRAYS is not set, are a single subscript or any colon
>      modifiers appearing after the name, or any of the characters '^',
>      '=', '~', '#' or '+' appearing before the name, all of which work
>      with or without braces.
>
> Note "exceptions ... are a single subscript or any colon modifiers
> appearing after the name ..."

The braces are required unless(1) they are not(2) required
unless(3) some option is not(4) present.  Quadruple negative.

All I understand is that it's trial and error to figure out
whether braces are required.  Let's see:

  $ rm -rf tmp
  $ mkdir tmp
  $ cd tmp
  $ touch a b

  $ unsetopt KSH_ARRAYS
  $ echo ^a =ls
  b /bin/ls                                <-- matches man page
  $ echo ~a
  zsh: no such user or named directory: a  <-- matches man page
  $ X=abc; echo $X:u $X[2]
  ABC B                                    <-- matches man page
  $ echo $+X $#X
  1 3                                      <-- matches man page

  $ setopt KSH_ARRAYS
  $ echo ^a =ls
  b /bin/ls                      <-- not what the man page says
  $ echo ~a                                     |
  zsh: no such user or named directory: a  <----|
  $ X=abc; echo $X:u                            |
  ABC                            <-- ok         |
  $ echo $X[2]                                  |
  zsh: no matches found: ABC[2]  <-- ok         |
  $ echo $+X $#X                                |
  1 3                            <--------------

--

This section could be split in two, to improve readability:

  ${NAME}
       The value, if any, of the parameter NAME is substituted.
       This form is required if the characters following the closing
       brace would be interpreted as part of the substitution
       otherwise.

  $NAME
       The value, if any, of the parameter NAME is substituted.  A
       letter, digit or underscore after NAME is interpreted as part
       of NAME. The characters '^', '=', '~', '#' or '+' appearing
       before NAME work without braces.  A single subscript or any
       colon modifiers appearing after NAME work only if the
       KSH_ARRAYS option is not set.  All other kinds of complex
       substitutions require the form with braces.  Using braces is
       always safe.

And adding a short section about modifiers may be a good idea:

  PARAMETER EXPANSION
      ...

    Modifiers
      Modifiers preceded ba a `:' as described in  the  section
      `Modifiers' in the section `History Expansion' can be applied.
      If the KSH_ARRAYS option is set, braces are required to use
      modifiers.

    Parameter Expansion Flags
      ...

It's a slight duplication of information, but much easier to find
for normal mortals.  (Note that the `:' is there on purpose to
make it easier to find when searching through the man page.)

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


  reply	other threads:[~2022-12-02  0:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-01 21:29 Dominik Vogt
2022-12-01 21:34 ` Eric Cook
2022-12-01 21:36   ` Bart Schaefer
2022-12-01 22:39     ` Dominik Vogt
2022-12-01 23:42       ` Bart Schaefer
2022-12-02  0:52         ` Dominik Vogt [this message]
2022-12-02  1:34           ` Bart Schaefer
2022-12-02  2:47             ` Dominik Vogt
2022-12-02 17:59               ` Bart Schaefer
2022-12-02  1:28         ` Dominik Vogt
2022-12-02  1:48           ` Bart Schaefer

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=Y4lMMptVvkj62FF3@localhost \
    --to=dominik.vogt@gmx.de \
    --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).