Re: Parameter Expansion
"Mark J. Reed" <[email protected]>
| Newsgroups | gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <CAA=-s3xT_v+hXkfAteLfvnL38JSsGo=d5dh0QZJrt1BmaDvJ_Q@mail.gmail.com> |
History expansion is the stuff you can do with *!* to retrieve values from
history and modify them in various ways. Those modifiers are so useful that
Zsh has added them to its parameter expansion. Doing a search and replace
with *:s/old/new/* is an example of a modifier that originated on the
history side of things, as are most of the ones that involve a colon
followed by a letter.
But the history expansion takes place in a different phase of evaluation
and doesn't expand any nested parameter values, which is why your attempt
didn't work - the value of SCMROOT doesn't contain the literal string
"$HOME", so nothing is changed.
For general search and replace, parameter expansion has its own
substitution syntax - ${param/old/new} will replace only the first
instance of "old" in the string, while $[param//old/new} will replace all
of them. Unlike with :s, you can't change the delimiter and so have to
escape any literal slashes in either the search or replacement values. But
any nested parameters will be expanded (and slashes in the expanded value
taken literally), so your attempt would work.
However, if all you want to do is remove stuff from the beginning or end of
a parameter value, there's targeted syntax for that, which Peter has
supplied. In general, ${param#prefix} will remove stuff from the beginning
of the string, and ${param%suffix} will remove stuff from the end. The
stuff to remove can be specified via a wildcard pattern (not a regex, but a
file glob), in which case a single *#* or *%* will remove the shortest
thing that matches while a double *##* or *%%* will remove the longest.
On Tue, Jul 15, 2025 at 11:13 AM Vin Shelton <[email protected]> wrote:
> Although I see history mentioned in the zshexpn manpage, I don't know what
> my example has to do with history.
>
> On Tue, Jul 15, 2025 at 11:01 AM Eric Cook <[email protected]> wrote:
>
>> On 7/15/25 10:44 AM, Vin Shelton wrote:
>> > I don't understand the order of parameter expansion. Consider the
>> following example:
>> >
>> > zsh -f
>> > export HOME=/home/acs
>> > smaug% export SCMROOT=/home/acs/scmroot
>> > smaug% echo ${SCMROOT:s-$HOME/--}
>> >
>> > This yields /home/acs/scmroot", but I expected "scmroot".
>> > TIA,
>> > Vin Shelton
>> >
>>
>> The history modifier doesn't create a new context where the parameter
>> expansion code recurses and resolves any parameters within it's operands.
>> It is searching for the literal string $HOME.
>>
>>
>
> --
> Whoa, I'm just surprised at how accurate that description of me really is:
> some old cowboy guy that used to shoot movies at Spahn Ranch
>
--
Mark J. Reed <[email protected]>