A NOTE has been added to this issue. ====================================================================== https://www.austingroupbugs.net/view.php?id=1941 ====================================================================== Reported By: dwheeler Assigned To: ajosey ====================================================================== Project: 1003.1(2024)/Issue8 Issue ID: 1941 Category: Shell and Utilities Type: Enhancement Request Severity: Objection Priority: normal Status: Under Review Name: David A. Wheeler Organization: User Reference: Section: grep Page Number: 1 Line Number: 1 Interp Status: --- Final Accepted Text: ====================================================================== Date Submitted: 2025-08-30 21:51 UTC Last Modified: 2025-09-15 02:03 UTC ====================================================================== Summary: Add widely-implemented options to grep ======================================================================
---------------------------------------------------------------------- (0007259) dwheeler (reporter) - 2025-09-15 02:03 https://www.austingroupbugs.net/view.php?id=1941#c7259 ---------------------------------------------------------------------- <p>Here’s my analysis on <code>grep -C</code>. Does anyone have guidance on what should go in to POSIX?</p> <p>I believe at <em>least</em> <code>grep -CNUM</code> (with NUM <em>required</em>) should be standardized. The <code>grep -CNUM</code> format is widely used and essentially universal. 8 of 9 systems I examined support the grep <code>-C</code> option, and all of those support the <code>-CNUM</code> format (no space). The one exception was Oracle Solaris, which lacks <code>-C</code> but could add it. Solaris hasn’t had an update in 7 years and is mostly-unmaintained, so its failure to support it shouldn’t block standardization.</p> <p>There’s a <em>strong</em> case to be made for <code>grep -C NUM</code> as well. This is <em>widely</em> supported (7 of 9), and Solaris could add it. Many systems’ man pages are misleading, for example, Apple MacOS 13.7.8 says “no whitespace may be given between the argument and its option” but in fact this is false; whitespace <em>is</em> supported between <code>-C</code> and the <code>NUM</code> argument. I suspect GNU did this first, and most of the BSDs have added support for it over time. I’ve verified through testing that this works on GNU, MacOS, BusyBox, ToyBox, FreeBSD, and NetBSD. The Illumos source code makes me believe it works there too.</p> <p><em>However</em> the <code>grep -C NUM</code> syntax does <em>NOT</em> work on OpenBSD (I verified through testing), and it <em>would</em> be incompatible with their existing use (where it is <em>truly</em> optional). It’d be cleaner to support <code>grep -C NUM</code> everywhere, but I understand avoiding unnecessary backwards incompatibility. I could live with just <code>grep -CNUM</code> being in the standard. I’d love feedback/guidance on this.</p> <p>Systems I analyzed:</p> <ol type="1"> <li>GNU grep version 3.11 (Linux)</li> <li>macOS grep version 13.7.8 (BSD-based)</li> <li>OpenBSD grep version 7.7 (the only one that doesn’t support <code>-C NUM</code> and requires <code>-CNUM</code>)</li> <li>FreeBSD grep version 14.3. See: https://man.freebsd.org/cgi/man.cgi?query=grep&apropos=0&sektion=0&manpath=FreeBSD+14.3-RELEASE+and+Ports&arch=default&format=html</li> <li>NetBSD grep version 10.1</li> <li>Oracle Solaris 11 grep Documentation: https://docs.oracle.com/cd/E88353_01/html/E37839/grep-1.html</li> <li>BusyBox grep version 1.1.36</li> <li>ToyBox grep version 0.8.9</li> <li>Illumos grep - source https://github.com/illumos/illumos-gate/blob/master/usr/src/cmd/grep/grep.c.</li> </ol> <p>I don’t have a Solaris or Illumos system; I installed and ran a test script on everything else. Here’s my test script: https://dwheeler.com/misc/test_grep_c.sh</p> <p>Notes:</p> <ul> <li>For busybox and toybox I installed their packages on Debian and then ran “busybox grep” and “toybox grep” to check their support.</li> <li>I haven’t been able to fully install OpenBSD on my system, but from the installer I could drop to a shell and use its “grep”.</li> </ul> Issue History Date Modified Username Field Change ====================================================================== 2025-08-30 21:51 dwheeler New Issue 2025-08-30 21:51 dwheeler Status New => Under Review 2025-08-30 21:51 dwheeler Assigned To => ajosey 2025-08-30 21:56 dwheeler Note Added: 0007240 2025-08-30 21:59 dwheeler Note Added: 0007241 2025-08-31 00:07 mirabilos Note Added: 0007242 2025-08-31 00:10 mirabilos Note Added: 0007243 2025-08-31 21:52 dwheeler Note Added: 0007244 2025-08-31 22:01 dwheeler Note Added: 0007245 2025-09-01 05:57 stephane Note Added: 0007246 2025-09-01 06:05 stephane Note Added: 0007247 2025-09-01 15:36 dwheeler Note Added: 0007249 2025-09-01 17:10 dwheeler Note Added: 0007250 2025-09-01 17:18 dwheeler Note Added: 0007251 2025-09-11 15:31 lanodan Note Added: 0007253 2025-09-11 15:36 lanodan Note Edited: 0007253 2025-09-11 15:37 lanodan Note Edited: 0007253 2025-09-11 15:37 lanodan Note Edited: 0007253 2025-09-11 15:50 geoffclare Project 1003.1(2008)/Issue 7 => 1003.1(2024)/Issue8 2025-09-11 18:01 dwheeler Note Added: 0007256 2025-09-12 16:28 stephane Note Added: 0007258 2025-09-15 02:03 dwheeler Note Added: 0007259 ======================================================================
