Bird
0
0

What will be the exit code of this script?

medium📝 Command Output Q13 of 15
Bash Scripting - Error Handling
What will be the exit code of this script?
#!/bin/bash
if [ -f "/tmp/file.txt" ]; then
  exit 3
else
  exit 0
fi
A0 if /tmp/file.txt exists, 3 if not
B3 if /tmp/file.txt exists, 0 if not
CAlways 0
DAlways 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand the if condition

    The script checks if the file /tmp/file.txt exists using -f.
  2. Step 2: Analyze exit codes for each case

    If the file exists, it exits with code 3; otherwise, it exits with 0.
  3. Final Answer:

    3 if /tmp/file.txt exists, 0 if not -> Option B
  4. Quick Check:

    File exists -> exit 3; else -> exit 0 [OK]
Quick Trick: Match exit code to condition true/false carefully [OK]
Common Mistakes:
MISTAKES
  • Mixing up the if and else branches
  • Assuming exit 0 always means file exists
  • Ignoring the file check condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes