Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Modifiers and parameter expansion?
- X-seq: zsh-users 28470
- From: Dominik Vogt <dominik.vogt@xxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Modifiers and parameter expansion?
- Date: Fri, 2 Dec 2022 01:52:02 +0100
- Archived-at: <https://zsh.org/users/28470>
- In-reply-to: <CAH+w=7b5rQNkfQcRb1zVDbD5CMn-8F67pGS9JvshqyRiFF6nXw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: zsh-users@xxxxxxx
- References: <Y4kcyW/acG5cPz+o@localhost> <1dcdd69c-ce29-921f-f0f0-6862f68b62a6@gmx.com> <CAH+w=7amodhidc8i1MqPWkqhYv2PkN56WmZQ8e-g-GU6p3F6GA@mail.gmail.com> <Y4ktCdAPhIVR7bGk@localhost> <CAH+w=7b5rQNkfQcRb1zVDbD5CMn-8F67pGS9JvshqyRiFF6nXw@mail.gmail.com>
- Reply-to: dominik.vogt@xxxxxx
- Ui-outboundreport: notjunk:1;M01:P0:8N8yH3O1XcE=;A9SzXwMgHTpOgKfLezaVWNHEzwv SkW8AwkMnQ48NTVa6iQVYMIP6evbmHnVVcuiw4t+guelyrFvtOcRgC8YgqqRuALMZ8YGIfypi nKUkzpunFTiM1Ys+sRteyABh8xiHIb6zbjA5R0ZB8VrWaTF5QUaLJ2UxXWfcJqdSIGG9JfngC gg2QNIOcDsX0Jyk63dTxR1+zBfBFnFmn8+npkRY+5F5+0+oQc3Z4c3GhB0FzPICo3YoCtsla+ 1P0VVxWiWp4M9FAIW/zt1prX4PEJfAHEYsnYdwnmaDEMIrWvlg8ZMEXGnjcOKeCi+rcY0k8Qh oC+UZsGuNJVTN2qolHpWNZN4U9Z4tgodvOogCE4iOAdHLywnjPYvHt9cVB9kDFy9X4lhrOPWx oFRH+AnSbHJ3kJe4ViwRW777w3eYkaKtJ4cNrAcStaGgHD0PptcbGvP72sMXZCcdYMLz400H/ ZrmQOTnLRFuOoE0DnsZspDxu+O9Y7FnDjXsKfpog3JVU8h3yxEEmzZcRmu0FlVZClCsfjwtYx 3rUiVo6T8woegcfk//RtxWBeZNdNBwa0ow1g3DzW+fbrSSnEFqEytV+Y9t4AEuuy6pn3yYAQ9 w1De7gxDVcqlOWdRXM2CFhjt/ywi38rgX7+keapld6u+v8P8X+SBV0hsROpFcIex+50c7jP8D 9D2LjEGHqx7onxy4ghFJn4WoBg/Fk1HdU7B93+6nd+nfYNrwk5soV7k5n+Q4utJy7DGBuRz3P so1IBhxw4NMf6gkmgW0nzxSzLvRQTAVoLqqPIqJqLQhCCRJx7inRjQiiVE5x5yIn8khhGWiwH 4wxeEa6DagbTo+cTQa1g5SwXW/QuzsI5aYcgviIDRL52D+vCwkx15nfr3Xzfpdxg3RKx1qzo9 wMAhm65nYH+BDm+Txx5jxQltHeY43VySL9mxJ0iNeQrlGL/tBzBUc+O0EXA+vSkQVACRmSmf/ sy/eOA==
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