Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: netbsd
Compiler: gcc
Compilation CFLAGS: -O2 -D_FORTIFY_SOURCE=2 -I/usr/include
-Wno-parentheses -Wno-format-security
uname output: NetBSD cq60-615dx.blilly.net 9.0 NetBSD 9.0 (GENERIC)
#0: Fri Feb 14 00:06:28 UTC 2020
mkre...@mkrepro.netbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC
amd64
Machine Type: x86_64--netbsd

Bash Version: 5.0
Patch Level: 17
Release Status: release

Description:
       Bash parameter expansion (remove largest trailing match, remove
largest leading match, pattern replacement) does not work
Tested on OpenSUSE Leap 15.2, bash version 4.4.2.3(1)-release
(x86_64-suse-linux-gnu)
OpenBSD 6.7 bash version 5.0.17(1)-release (x86_64-unknown-openbsd6.7)
NetBSD 9.0 bash version 5.0.17(1)-release (x86_64--netbsd)
FreeBSD 12.1-STABLE bash version 5.0.18(2)-release (amd64-portbld-freebsd12.1)

Same results in all cases; this report posted from NetBSD 9.0.

As an example, consider constructing a path from components; the full
path should
have a single separator (slash), so any stray slashes at the tail of
the first part or the
start of the last part should be elided.  There are many ways that
that can be done,
the "Repeat By" section demonstrates only a few examples.

Relevant manual sections are:
https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Pattern-Matching

Repeat-By:
 $ bash shellbug
Running bash from /usr/pkg/bin/bash
        Bash version 5.0.17(1) release GNU
dir=/foo/bar/baz/// separator1=/ separator2(octal escape)=\057
number0=* number1=+ pattern1=${number1}(${separator1})=+(/)
pattern2=${number0}([${separator2}])=*([\057]) base=///grimble/pritz
path=${dir%%${pattern1}}${separator1}${base##${pattern2}}=${dir%%+(/)}/${base##*([\057])}=/foo/bar/baz///////grimble/pritz
path=${dir/%${pattern1}/${separator1}}${base/#${pattern2}/}=${dir/%+(/)//}${base/#*([\057])/}=/foo/bar/baz//////grimble/pritz

Fix:
(demonstrates how a functioning shell handles exactly the same cases):
$ ksh93 shellbug
Running ksh93 from /usr/pkg/bin/ksh93
        $Id: Version AJM 93u+ 2012-08-01 $
dir=/foo/bar/baz/// separator1=/ separator2(octal escape)=\057
number0=* number1=+ pattern1=${number1}(${separator1})=+(/)
pattern2=${number0}([${separator2}])=*([\057]) base=///grimble/pritz
path=${dir%%${pattern1}}${separator1}${base##${pattern2}}=${dir%%+(/)}/${base##*([\057])}=/foo/bar/baz/grimble/pritz
path=${dir/%${pattern1}/${separator1}}${base/#${pattern2}/}=${dir/%+(/)//}${base/#*([\057])/}=/foo/bar/baz/grimble/pritz

Reply via email to