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

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

Machine-checked — no person has signed for it. This question was read against the source cited below by an automated pass, which found no contradiction. That is a weaker claim than it sounds: the same kind of process wrote the question, so it can confirm its own mistake.

Treat it as a good draft rather than as settled fact, and read the source below before you rely on it. It is not used in mock exams here — only questions a person has signed for are.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

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 short set from objective 103 and space the ones you get wrong.

Practise LPIC-1 Exam 101-500

More questions on this objective

All questions on GNU and Unix commands

Practise LPIC-1 Exam 101-500