Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Modifiers and parameter expansion?



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@xxxxxx> 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




Messages sorted by: Reverse Date, Date, Thread, Author