GNU bug report logs - #78891
Option to make grep always exit with 0

Previous Next

Package: grep;

Reported by: Thomas Güttler Mailinglisten <guettliml <at> gmail.com>

Date: Tue, 24 Jun 2025 14:36:04 UTC

Severity: normal

Full log


Message #11 received at 78891 <at> debbugs.gnu.org (full text, mbox):

From: Thomas Güttler Mailinglisten <guettliml <at> gmail.com>
Cc: 78891 <at> debbugs.gnu.org
Subject: Re: bug#78891: Option to make grep always exit with 0
Date: Wed, 25 Jun 2025 07:28:49 +0200
[Message part 1 (text/plain, inline)]
Hi Dale, thank you for your reply. Currently, I work around like this:

echo -e "foo\nbar" | { grep '^#' >comments.txt || true; } |
some-other-command

Related: My Bash Strict Mode. Handle non-zero exit codes
<https://github.com/guettli/bash-strict-mode/blob/main/README.md#bash-strict-mode-handle-non-zero-exit-codes>

This works, but it is not nice to read and more typing.

There are many ways to work around that. During the last months I wrote
a lot of Bash code, and often I needed to handle the non-zero exit code of
grep.

It would be handy to have an option to make grep always return zero (except
"file not found" or similar errors).

Why not?

Before creating a patch, I would like to know which command line option you
prefer.

There are not many characters left, which could be used.

What about `-g` like "good, even if nothing was found"?

Or `-t` like always return true (zero exit status).

But first: Would a patch be acceptable?

Regards,
  Thomas



Am Di., 24. Juni 2025 um 20:22 Uhr schrieb Dale R. Worley <
Dale.Worley <at> comcast.net>:

> Thomas Güttler Mailinglisten <guettliml <at> gmail.com> writes:
> > To make Bash more robust, I use the "strict mode".
> >
> > This works fine, except that very often I want to filter out some
> > output, and it does not matter to me if there is a match or not.
> >
> > Afaik there is no option to make `grep` always exit with a zero exit
> > status. No matter if a match was found or not. Errors like "file not
> found"
> > should still return a non-zero exit status.
>
> Well, this isn't what you asked for, but it's a reasonaby terse way to
> get the effect without modifying grep:
>
>     grep ... || [[ $? != 2 ]]
>
> That has an exit status of 1 if grep exits with 2, and an exit status of
> 0 otherwise.
>
> Dale
>
[Message part 2 (text/html, inline)]

This bug report was last modified 50 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.