Run grep -q '^enabled$' state.txt.
Run echo DISABLED only when there is no exact match.
Which command list is correct?
In an OR list, || runs the right-hand command only when the left-hand command fails. grep -q returns failure when there is no match.
Detailed explanation
grep -q '^enabled$' state.txt && echo DISABLEDIncorrect. && runs the right side after a successful match.
Incorrect. && runs the right side after a successful match.
grep -q '^enabled$' state.txt || echo DISABLEDCorrect. echo runs when grep finds no matching line.
Correct. echo runs when grep finds no matching line.
grep -q '^enabled$' state.txt; echo DISABLEDIncorrect. ; prints DISABLED for both a match and a non-match.
Incorrect. ; prints DISABLED for both a match and a non-match.
grep -q '^enabled$' state.txt | echo DISABLEDIncorrect. A pipe connects output streams; it does not test grep's status this way.
Incorrect. A pipe connects output streams; it does not test grep's status this way.
Try it yourself
An example you can run in a temporary verification environment.
LAB_FILE=$(mktemp)
printf 'disabled
' > "$LAB_FILE"
grep -q '^enabled$' "$LAB_FILE" || echo DISABLED
rm "$LAB_FILE"Expected result
DISABLEDKey points
- Short-circuit evaluation with ||
- grep -q exit status
- Fallback handling
Notes
- Environment: Bash 5.2 / 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.