bin/166842: bsdgrep inconsistently handles ^ in non-anchoring positions

Jim Pryor dubiousjim at
Wed Apr 11 13:50:13 UTC 2012

>Number:         166842
>Category:       bin
>Synopsis:       bsdgrep inconsistently handles ^ in non-anchoring positions
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Apr 11 13:50:12 UTC 2012
>Originator:     Jim Pryor
>Release:        9.0-PRELEASE
FreeBSD 9.0-PRERELEASE FreeBSD 9.0-PRERELEASE #0: Tue Nov 29 02:45:33 EST 2011     root at  amd64
version line:
/*      $FreeBSD: src/usr.bin/grep/grep.c,v 2011/10/20 16:08:11 gabor Exp $

According to the POSIX-2008 standard, "^" and "$" should be ordinary characters in BREs (basic regexs) when they're not in anchoring positions (as contrasted to EREs, where they should always be anchors). Hence:

$ printf 'a^b$c' | grep -o 'a^b'

should match, and it does when I use Gnu grep (on Linux), and using BusyBox grep (again on Linux, built against uClibc). But it doesn't using the described version of FreeBSD grep. Curiously though:

$ printf 'a^b$c' | grep -o '[a]^b'

will match. And so too will 'b$c'.

One can't portably rely on '\^' here to specify the literal '^', because POSIX-2008 says that '^' in non-anchoring positions is not special in BREs, and that the combination of '\' and a non-special character is undefined. Of course, neither can one use '[^]'.

See above.


More information about the freebsd-bugs mailing list