Skip to content

Add SC2337: warn about grep early exit in pipelines under pipefail - #3498

Open
mclasmeier wants to merge 6 commits into
koalaman:masterfrom
mclasmeier:feature/grep-q-pipefail
Open

Add SC2337: warn about grep early exit in pipelines under pipefail#3498
mclasmeier wants to merge 6 commits into
koalaman:masterfrom
mclasmeier:feature/grep-q-pipefail

Conversation

@mclasmeier

@mclasmeier mclasmeier commented Jul 21, 2026

Copy link
Copy Markdown

Adds a new linter rule about using grep -q in combination with set -o pipefail

When set -o pipefail is active and grep -q (or --quiet) appears as a non-first command in a pipeline, the upstream command may receive SIGPIPE because grep -q exits immediately upon finding a match. This causes the pipeline to report a non-zero exit status under pipefail.

The check covers grep, egrep, and fgrep variants. It does not warn when grep -q reads from a file directly or is the first command in the pipeline.

Please note, my Haskell skills have become a bit rusty -- did this together with Claude.
Is there anything missing for this contribution?

When `set -o pipefail` is active and `grep -q` (or `--quiet`) appears
as a non-first command in a pipeline, the upstream command may receive
SIGPIPE because grep -q exits immediately upon finding a match. This
causes the pipeline to report a non-zero exit status under pipefail.

The check covers grep, egrep, and fgrep variants. It does not warn
when grep -q reads from a file directly or is the first command in
the pipeline.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@e-kwsm e-kwsm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives false positive to

grep -e -q  # search "-q"

Also, there are other cases where SIGPIPE is sent:

  • grep -m 𝑛 -e 𝑝𝑎𝑡
  • head
  • sed’s quit e.g. sed -n -e '/𝑝𝑎𝑡/{p;q;}'
  • nawk’s exit e.g. nawk '$0 ~ /𝑝𝑎𝑡/ { print; exit; }'

@mclasmeier

Copy link
Copy Markdown
Author

@e-kwsm Hi, thanks for the review.
Regarding the other cases (head, sed, etc.), I would consider this out of scope for this PR?
Are you generally positive about this contribution, once false positives are addressed?

@mclasmeier

Copy link
Copy Markdown
Author

@e-kwsm I have pushed a commit addressing the false positives by using getOpts instead.

@mclasmeier
mclasmeier requested a review from e-kwsm July 22, 2026 16:10

@e-kwsm e-kwsm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following non-POSIX options are not covered:

  • grep -m 𝑛 -e pat (GNU and BSD)
  • grep -L -e pat (GNU sends SIGPIPE, while BSD does not)

@e-kwsm

e-kwsm commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Regarding the other cases (head, sed, etc.), I would consider this out of scope for this PR?

So SC2337 covers grep only, and SC2338? should deal with head?

@mclasmeier

Copy link
Copy Markdown
Author

@e-kwsm Updated to also detect grep -m n and grep -L.
Indeed, I would leave other tools such as head, for other PRs.

@mclasmeier
mclasmeier requested a review from e-kwsm July 24, 2026 07:05
Comment thread src/ShellCheck/Checks/Commands.hs Outdated
@e-kwsm

e-kwsm commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

This will fix #665.

@mclasmeier
mclasmeier requested a review from e-kwsm July 27, 2026 08:14
@e-kwsm
e-kwsm requested review from brother and koalaman July 27, 2026 14:57
Comment thread src/ShellCheck/Checks/Commands.hs Outdated
@mclasmeier
mclasmeier requested a review from e-kwsm July 28, 2026 10:45
@mclasmeier mclasmeier changed the title Add SC2337: warn about grep -q in pipelines under pipefail Add SC2337: warn about grep early exit in pipelines under pipefail Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants