Subject: bug#78891: Option to make grep always exit with 0

From: Thomas Güttler Mailinglisten <[email-masked]>
Date: Wed, 25 Jun 2025 07:28:49 +0200

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 <
[email-masked]>:

> Thomas Güttler Mailinglisten <[email-masked]> 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
>