Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Aug 2009 03:26:50 +0400
From:      Eygene Ryabinkin <rea-fbsd@codelabs.ru>
To:        Doug Barton <dougb@FreeBSD.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Problem in bin/sh stripping the * character through ${expansion%}
Message-ID:  <ruze2YSbnZSTRbUbUJTTZyWDlyA@PR865FKBRXbdFBYZMkH1tmebpCc>
In-Reply-To: <4A7B1DB0.1040602@FreeBSD.org>
References:  <4A7B1DB0.1040602@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Doug, good day.

Thu, Aug 06, 2009 at 11:15:12AM -0700, Doug Barton wrote:
> I came across this problem during a recent portmaster update. When
> trying to strip off the * character using variable expansion in bin/sh
> it doesn't work. Other "special" characters do work if they are
> properly escaped.
> 
> The attached mini-script clearly shows the problem:
> 
> $ sh sh-strip-problem
> var before stripping: foo\*
> var after stripping: foo\*
> 
> var before stripping: foo\$
> var after stripping: foo\

According to the sh(1), it is not a problem.  Namely,
 - \* being unquoted at all will produce a lone '*';
 - '*' when treated as the smallest pattern, will result in a stripping
   of a zero-length string -- it is the smallest pattern in the case of
   '*' that matches anything.

In order to strip the trailing star you should use
-----
var=${var%[*]}
-----
This gives you the pattern of '[*]' that is properly treated as the
single star -- it's a weird way to escape the star in the patterns.

Other characters work, but only because they produce no patterns.
In principle, you can try \? that will remove any trailing character
no matter what, since it is the second character that will produce
the pattern.

> In contrast, bash does the right thing:
> 
> bash sh-strip-problem
> var before stripping: foo\*
> var after stripping: foo\
> 
> var before stripping: foo\$
> var after stripping: foo\

I will try to look at the XCU to understand what is the POSIX
way of doing the things.
-- 
Eygene
 _                ___       _.--.   #
 \`.|\..----...-'`   `-._.-'_.-'`   #  Remember that it is hard
 /  ' `         ,       __.--'      #  to read the on-line manual
 )/' _/     \   `-_,   /            #  while single-stepping the kernel.
 `-'" `"\_  ,_.-;_.-\_ ',  fsc/as   #
     _.-'_./   {_.'   ; /           #    -- FreeBSD Developers handbook
    {_.-``-'         {_/            #



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?ruze2YSbnZSTRbUbUJTTZyWDlyA>