Bird
0
0

What will be the output of this script?

medium📝 Command Output Q5 of 15
Bash Scripting - Error Handling
What will be the output of this script?
grep 'hello' /dev/null
 echo $?
A1
B0
C2
D127
Step-by-Step Solution
Solution:
  1. Step 1: Understand grep behavior on empty input

    Searching 'hello' in /dev/null (empty file) returns no matches but no error.
  2. Step 2: Check grep exit code for no matches

    grep returns 1 if no lines matched, even when searching /dev/null as it provides empty input.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    grep no matches = 1 [OK]
Quick Trick: grep returns 1 if no matches, 0 if matches [OK]
Common Mistakes:
MISTAKES
  • Thinking no matches means exit code 0
  • Confusing error with no match

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes