bin/166842: bsdgrep inconsistently handles ^ in non-anchoring
dubiousjim at gmail.com
Wed Apr 11 13:50:13 UTC 2012
>Synopsis: bsdgrep inconsistently handles ^ in non-anchoring positions
>Arrival-Date: Wed Apr 11 13:50:12 UTC 2012
>Originator: Jim Pryor
FreeBSD vaio.jimpryor.net 9.0-PRERELEASE FreeBSD 9.0-PRERELEASE #0: Tue Nov 29 02:45:33 EST 2011 root at vaio.jimpryor.net:/usr/obj/usr/src/sys/MINE amd64
/* $FreeBSD: src/usr.bin/grep/grep.c,v 18.104.22.168 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 '[^]'.
More information about the freebsd-bugs