Skip to content
Latchkey

grep Command Reference for CI Scripts

grep prints the lines that match a pattern and exits non-zero when none do.

grep is the most-used filter in CI. Its defining gotcha: "no match" is exit code 1, which under set -e or in a pipeline aborts the whole job.

Common flags/usage

  • -r / -R: recurse into directories
  • -i: case-insensitive match
  • -E: extended regex (like egrep)
  • -q: quiet, return the exit status only
  • -v: invert the match
  • -c / -o / -l: count / only-matching / filenames

Example

shell
if grep -rq "console.log" src/; then
  echo "Found stray debug logging" && exit 1
fi
grep -E '^(feat|fix|chore):' <<< "${COMMIT_MSG}" || exit 1

In CI

A grep that finds nothing exits 1, and with set -e that aborts the job even when "no matches" was the desired result; guard it with grep -q X file || true or test the status explicitly. Exit 2 is a real error (bad path or regex). grep -P (Perl regex) is GNU-only and absent on BusyBox.

Key takeaways

  • grep exits 1 on no match, which aborts the step under set -e unless guarded.
  • Use -q when you only need the match/no-match exit status.
  • Exit code 2 is a real error; 1 just means nothing matched.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →