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 1In 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
sed Command Reference for CI Scriptssed is a stream editor for find-and-replace in CI builds. Reference for -i, -e, -E, and s///, plus the GNU-vs…
awk Command Reference for CI Scriptsawk is a pattern-scanning text processor for CI. Reference for -F field separator, -v variables, and $1/$NF f…
find Command Reference for CI Scriptsfind walks directory trees and acts on matching files in CI. Reference for -name, -type, -exec, and -print0,…
xargs Command Reference for CI Scriptsxargs builds and runs commands from stdin in CI. Reference for -0, -n, -P, -I, and -r, plus the empty-input t…