Skip to content
Latchkey

grep -q: Quiet Asserts on Command Output

grep -q searches silently and exits 0 as soon as it finds a match, making it a clean assertion primitive.

When you only care whether a pattern is present, -q gives you a boolean without cluttering the log. It is the idiomatic way to assert on output in CI.

What it does

grep -q (--quiet, also --silent) suppresses all normal output and exits with status 0 on the first match, stopping the scan early. It is built for use in conditionals where you want the result, not the matching lines. Combine it with -v to assert that a pattern is absent.

Common usage

Terminal
if grep -q "healthy" status.txt; then
  echo "service is up"
fi

# assert a forbidden string is NOT present
! grep -q "TODO" release-notes.md

# check piped output quietly
docker logs app 2>&1 | grep -q "Listening on"

Options

FlagWhat it does
-q / --quiet / --silentPrint nothing; set exit code only
-m N / --max-count=NStop after N matches (pairs well with -q)
-vInvert, to assert a pattern is absent
-FTreat the pattern as a fixed string

In CI

Use grep -q inside if to branch and ! grep -q to fail when something forbidden appears. Because -q exits on the first match, it is also efficient on huge logs. When reading from a pipe under set -o pipefail, -q exiting early can send SIGPIPE upstream, so wrap the producer if it must finish cleanly.

Common errors in CI

A surprising "Broken pipe" or a non-zero status from the command feeding grep -q comes from grep closing the pipe early after its first match; this is expected behavior, not a grep error. If you need the producer to run to completion, drop -q or buffer its output first. Forgetting -q and using bare grep in an if works too, but it leaks the matched lines into the log.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →