In `grep '\(ab\)\1' file`, what is the role of `\1`?

LPIC-1 Exam 101-500, objective 103. GNU and Unix commands hard

Draft — not checked yet. This question was written from the published exam objectives and cites the clause it comes from, but nobody has yet read it against that clause and signed for it.

It is kept out of search results and out of mock exams until they have. Read the source below before you take the answer as settled.

The options

Not correct It matches any single character, like a shorthand for the dot.

Wrong. The dot matches any character; \1 is strictly a reference to a previously captured group.

Correct A backreference: it must match the same text the first \( \) group captured, so the line must contain 'abab'.

Correct. \1 refers back to what group one actually matched, which is how you express 'the same text again'.

Not correct It repeats the group once more regardless of what it matched, so 'abxy' would match.

Wrong. A backreference is tied to the captured text, not to the pattern. It cannot match a different string than the group did.

Not correct It is the sed replacement reference and has no meaning in grep.

Wrong. \1 is indeed used in sed replacements, but backreferences are also valid inside a search pattern: POSIX defines them for basic regular expressions, and GNU grep supports them with -E as well.

Why

Backreferences \1 to \9 match the exact text an earlier group captured. In basic regular expressions the group itself is written \( \); with grep -E it is written ( ) while the reference stays \1. A classic use is finding doubled words with GNU grep: grep -E '\b(\w+) \1\b'.

Where this comes from

Cited
LPI exam objective 103.7
What it says
Use grouping and backreferences in regular expressions.

Practise this

Reading one question is not practice. The trainer will draw a set from objective 103 and space the ones you get wrong.

Practise LPIC-1 Exam 101-500