Bird
0
0

What will this script print if mydir is a directory?

medium📝 Command Output Q5 of 15
Bash Scripting - File Operations in Scripts
What will this script print if mydir is a directory?
if [ -f mydir ]; then echo "File"; else echo "Not a file"; fi
ANot a file
BFile
CSyntax error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand -f operator

    The -f operator returns true only if the path is a regular file.
  2. Step 2: Apply to directory

    Since mydir is a directory, -f mydir returns false, so else branch runs.
  3. Final Answer:

    The script prints "Not a file" -> Option A
  4. Quick Check:

    -f false for directories = else branch [OK]
Quick Trick: -f is false for directories, true only for regular files [OK]
Common Mistakes:
MISTAKES
  • Assuming -f true for directories
  • Confusing -f with -d
  • Ignoring else branch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes