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

Re: param modifier to strip _all_ suffixes?

On Mar 22,  9:07am, Sven Wischnowsky wrote:
} Subject: Re: param modifier to strip _all_ suffixes?
} Sweth Chandramouli wrote:
} > 	is there a token modifier like :r that strips _all_ 
} > suffixes of the form `.xxx', rather than just the last one--the 
} > equivalent of ${PARAM%%.*} instead of just ${PARAM%.*}?
} Combine it with `f':
}   % a=foo.a.b.c
}   % echo $a:fr
}   foo

To summarize a private discussion that Sweth and I had about this ...

First, note that ${param:fr} and ${param%%.*} are NOT equivalent when
the string being operated upon begins with a dot.

	zsh% x=.foo.bar.baz
	zsh% echo ${x:fr}
	zsh% echo ${x%%.*}


To get the equivalent of :fr with pattern matching, you have to do some
silly stuff like this:

	zsh% echo ${${${(M)x#(?|.)*.}:-$x}%.}

Unfortunately, :f and a few other operators do not apply to history
expansions.  There are two entirely separate bits of code implementing
the modifiers for parameters and those for history; no one has ever
bothered to extend the history modifier code beyond the initial csh
compatibility stuff.  I've forgotten the technical reason why there
can't be a single string-chopping function that implements all the
colon-modifiers and is simply used for both history and parameters.

A workaround for this is to employ a scratch variable to capture the
history expansion:

	zsh% cp .foo.bar.baz .florp.gletch.ick
	zsh% echo copied ${${x::="!:1"}:fr} to ${${x::="!$"}:fr}

That works for performing pattern substitutions on history as well, of
course, but that gets ugly pretty fast.

A last observation is that this stuff probably isn't as necessary in
history anyway, as you can always hit TAB to expand the history ref
and then edit it.  You're never going to be writing a script that uses
the history.  Nevertheless, consistency would be nice.

Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

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