bug-bash
[Top][All Lists]
Advanced

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

Re: String substitution bug


From: Andreas Kähäri
Subject: Re: String substitution bug
Date: Sun, 24 Nov 2024 09:05:42 +0100

On Sun, Nov 24, 2024 at 02:29:01AM +0000, marcel.plch via Bug reports for the 
GNU Bourne Again SHell wrote:
> On Sunday, November 24th, 2024 at 3:05 AM, Lawrence Velázquez <vq@larryv.me> 
> wrote:
> 
> > On Sat, Nov 23, 2024, at 7:11 PM, marcel.plch via Bug reports for the GNU 
> > Bourne Again SHell wrote:
> > 
> 
> > > I am trying to do some file management in bash and I have strings in
> > > this format:
> > > 
> 
> > > 1 dir/hello.txt
> > > 2 dir2/bar.jpg
> > > 
> 
> > > When I run this substitution:
> > > ${FOO/[:space:]*/Hello}
> > > I get this result:
> > > 1 dir/hHello
> > > 
> 
> > > The goal is to substitute everything after the first space (including
> > > the space) with Hello
> > > 
> 
> > > Seems like a bug to me.
> > 
> 
> > 
> 
> > It is not a bug. Your pattern is incorrect; you should be using
> > "[[:space:]]", not "[:space:]". The former is a bracket expression
> > containing the character class expression for the "space" character
> > class, while the latter is a bracket expression that matches any
> > of the characters ":", "s", "p", "a", "c", or "e".
> > 
> 
> > $ FOO='1 dir/hello.txt'
> > $ echo "${FOO/[[:space:]]*/Hello}"
> > 1Hello
> > 
> 
> > --
> > vq
> 
> Thank you for clarifictaion.
> 
> Maybe adding an extra clarification to the bash manpage
> in the Pattern Matching section would be a good idea?
> 
> I can imagine I'm not the only one who read this with
> a bit of misunderstanding, leading to a few lost hours.
> --
> Dormouse

I think the manual is quite clear:

        Within [ and ], character classes can be specified
        using the syntax [:class:], where class is one of the
        following classes defined in the POSIX standard:
        alnum alpha ascii blank cntrl digit graph lower print
        punct space upper word xdigit

It says that the syntax "[:class:]" may be used within "[" and "]". To
match a space-like character (including spaces, newlines and various
types of tabs), you would therefore use "[[:space:]]".

Would you want to match only a space, you would use a literal space.

-- 
Andreas (Kusalananda) Kähäri
Uppsala, Sweden

.



reply via email to

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