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

To reply to this bug, email your comments to 78891 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-grep <at> gnu.org:
bug#78891; Package grep. (Tue, 24 Jun 2025 14:36:05 GMT) Full text and rfc822 format available.

Acknowledgement sent to Thomas Güttler Mailinglisten <guettliml <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-grep <at> gnu.org. (Tue, 24 Jun 2025 14:36:05 GMT) Full text and rfc822 format available.

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

From: Thomas Güttler Mailinglisten <guettliml <at> gmail.com>
To: bug-grep <at> gnu.org
Subject: Option to make grep always exit with 0
Date: Tue, 24 Jun 2025 11:49:21 +0200
[Message part 1 (text/plain, inline)]
Hi,

after trying several tools like SaltStack and Ansible and several CI tools,
I came
to the conclusion that a Bash script is often the most convenient and
powerful solution.

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.

Just out of curiosity, would you accept a patch to implement that?

Which command line argument could be used for that option?

Regard,
  Thomas Güttler
[Message part 2 (text/html, inline)]

Information forwarded to bug-grep <at> gnu.org:
bug#78891; Package grep. (Tue, 24 Jun 2025 18:23:04 GMT) Full text and rfc822 format available.

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

From: "Dale R. Worley" <Dale.Worley <at> comcast.net>
To: 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: Tue, 24 Jun 2025 14:22:28 -0400
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




Information forwarded to bug-grep <at> gnu.org:
bug#78891; Package grep. (Wed, 25 Jun 2025 05:30:03 GMT) Full text and rfc822 format available.

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)]

Information forwarded to bug-grep <at> gnu.org:
bug#78891; Package grep. (Wed, 25 Jun 2025 06:10:02 GMT) Full text and rfc822 format available.

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

From: Martin Schulte <gnu <at> schrader-schulte.de>
To: 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 08:09:43 +0200
Hello Thomas!

> 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.

What about a

grep_always_true() {
  grep "$@" || true
}

?

Best regards,

Martin




Information forwarded to bug-grep <at> gnu.org:
bug#78891; Package grep. (Wed, 25 Jun 2025 17:17:02 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: 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 10:14:52 -0700
On 2025-06-24 22:28, Thomas Güttler Mailinglisten wrote:
>   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).

I dunno, there are several commands like grep that return exit status 
1/2 for different kinds of "failure". ("diff", for example.) I doubt 
whether we want to add such an option to all these commands.

"grep GREPARGS || test $? = 1" is short and easy to remember and has 
worked everywhere for decades. If you dislike its syntax you can package 
it inside a shell function.




Information forwarded to bug-grep <at> gnu.org:
bug#78891; Package grep. (Wed, 25 Jun 2025 17:52:02 GMT) Full text and rfc822 format available.

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

From: Dennis Clarke <dclarke <at> blastwave.org>
To: bug-grep <at> gnu.org
Subject: Re: bug#78891: Option to make grep always exit with 0
Date: Wed, 25 Jun 2025 13:51:15 -0400
On 6/25/25 13:14, Paul Eggert wrote:
> On 2025-06-24 22:28, Thomas Güttler Mailinglisten wrote:
>>   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).
> 
> I dunno, there are several commands like grep that return exit status 
> 1/2 for different kinds of "failure". ("diff", for example.) I doubt 
> whether we want to add such an option to all these commands.
> 
> "grep GREPARGS || test $? = 1" is short and easy to remember and has 
> worked everywhere for decades. If you dislike its syntax you can package 
> it inside a shell function.
> 
> 

I am at great risk of falling into a bike shed[1] here. If grep
works fine and all is happy then we get a zero return value. All
other possible situations *should* return a non-zero error status
that helps silly old UNIX people like me.

If someone needs to write if-then-else-what? clause stuff in code
then that is correct behavior. Why fix that which works fine?

-- 
--
Dennis Clarke
RISC-V/SPARC/PPC/ARM/CISC
UNIX and Linux spoken

[1] https://en.wikipedia.org/wiki/Law_of_triviality




Information forwarded to bug-grep <at> gnu.org:
bug#78891; Package grep. (Thu, 26 Jun 2025 00:51:01 GMT) Full text and rfc822 format available.

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

From: "Dale R. Worley" <Dale.Worley <at> comcast.net>
To: 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 20:49:49 -0400
Thomas Güttler Mailinglisten <guettliml <at> gmail.com> writes:
> Hi Dale, thank you for your reply. Currently, I work around like this:
>
> echo -e "foo\nbar" | { grep '^#' >comments.txt || true; } |
> some-other-command

The difficulty with that code is that it doesn't satisfy the requirement
you stated:

> 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.

That's why you need the test "$? != 2".

Dale




This bug report was last modified today.

Previous Next


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