Extract only lines that are exactly [prod] from labels.txt.
Exclude [prod]-old and prod, and treat the brackets as literal characters.
Which command is correct?
grep -F treats the pattern as a fixed string, and -x requires the whole line to match.
Detailed explanation
grep -Ex '[prod]' labels.txtIncorrect. -E treats [prod] as a character class and does not describe the literal line.
Incorrect. -E treats [prod] as a character class and does not describe the literal line.
grep -Fx '[prod]' labels.txtCorrect. -F preserves the brackets literally and -x requires the entire line.
Correct. -F preserves the brackets literally and -x requires the entire line.
grep -F '[prod]' labels.txtIncorrect. It finds the fixed substring anywhere, including in a longer line.
Incorrect. It finds the fixed substring anywhere, including in a longer line.
grep -x 'prod' labels.txtIncorrect. It omits the brackets from the pattern.
Incorrect. It omits the brackets from the pattern.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' '[prod]' '[prod]-old' prod p | grep -Fx '[prod]'Expected result
[prod]Key points
- -F means fixed string
- -x means whole-line match
- Brackets need no regex escaping in fixed mode
Notes
- Environment: GNU grep 3.x
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.