help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Character clearance in either end front or back


From: Alan D. Salewski
Subject: Re: Character clearance in either end front or back
Date: Thu, 8 Oct 2020 10:07:26 -0400
User-agent: Mutt/1.14.6 (2020-07-11)

On 2020-10-08 07:19:45, Greg Wooledge spake thus:
> On Thu, Oct 08, 2020 at 12:00:06PM +0200, Reuti wrote:
> > > Am 08.10.2020 um 08:06 schrieb Budi <budikusasi@gmail.com>:
> > > but merely
> > > clearing \\\  as in l=foo\\\
> > > 
> > > k=${%%\}
> > 
> > To remove all you will need a wildcard:
> > 
> > k=${l%%\\*}
> 
> No, that won't do it either.  You need an extended glob.
> 
> shopt -s extglob
> k=${l%%+(\\)}
> 
> Without extended globs, you could do it using a loop that removes one
> backslash at a time, but that's obviously going to be slower.

How about a two-step expansion[0]: generate (greedily) an exact
substring of the trailing char(s) to remove (if any), and then slice
it off (non-greedy):

k=${l%"${l##*[!\\]}"}

Not nearly as clean or clear as the extended glob approach, but
works cross-shell.

-Al


[0] I came across the approach in a stackoverflow.com discussion
    about trimming whitespace, which referenced the other sources
    below:

    http://www.jlaforums.com/viewtopic.php?t=1427403  (4th response, from Chris 
F.A. Johnson)
        referenced from: 
https://web.archive.org/web/20121022051228/http://codesnippets.joyent.com/posts/show/1816
            referenced from: https://stackoverflow.com/a/3352015

-- 
-----------------------------------------------------------------
a l a n   d.   s a l e w s k i                   salewski@att.net
                                               ads@salewski.email
                                      https://github.com/salewski
-----------------------------------------------------------------


reply via email to

[Prev in Thread] Current Thread [Next in Thread]