Bird
0
0

What will be the output of this script?

medium📝 Command Output Q13 of 15
Bash Scripting - Error Handling
What will be the output of this script?
ls /nonexistent_dir
 echo $?
A2
B0
CNo output
Dls: cannot access '/nonexistent_dir': No such file or directory
Step-by-Step Solution
Solution:
  1. Step 1: Understand the command behavior

    The command ls /nonexistent_dir tries to list a folder that does not exist, so it fails.
  2. Step 2: Check exit code after failure

    After failure, $? holds a non-zero exit code, 2 for ls missing directories.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Failed command exit code ≠ 0 [OK]
Quick Trick: Failed commands return non-zero exit codes like 2 [OK]
Common Mistakes:
MISTAKES
  • Assuming output is 0 for failure
  • Confusing error message with exit code
  • Expecting no output from echo

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes