Skip to content
Latchkey

grep -n: Show Line Numbers in Matches

grep -n prints the 1-based line number before each matching line.

When a CI check finds a problem, line numbers turn the output into something a developer can jump to. -n is the flag that adds them.

What it does

grep -n (--line-number) prefixes every output line with its line number within the file, separated by a colon. With multiple files or -r, grep also prints the filename; -H forces the filename prefix even for a single file, and -h suppresses it.

Common usage

Terminal
grep -n "TODO" main.go
grep -rn "FIXME" src/
grep -nH "deprecated" *.js

Options

FlagWhat it does
-n / --line-numberPrefix matches with the line number
-H / --with-filenameAlways print the filename
-h / --no-filenameNever print the filename
-T / --initial-tabAlign output with a leading tab
-b / --byte-offsetPrint the byte offset instead of/with line number

In CI

Use grep -rnH in lint-style checks so the log shows file:line:match, which many CI UIs and editors turn into clickable links. The format matches what tools like git grep and compilers emit, so downstream parsers handle it.

Common errors in CI

There is no real failure mode here beyond the universal no-match exit 1. One subtlety: line numbers count lines in each file independently, so when grepping concatenated output (e.g. from cat a b) the numbers restart per file only if grep sees them as separate arguments, not when they are merged in a pipe.

Related guides

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