Bird
0
0

What will be the output of this script if the current directory contains files: a.txt, b.txt, and c.log?

medium📝 Command Output Q5 of 15
Bash Scripting - Loops
What will be the output of this script if the current directory contains files: a.txt, b.txt, and c.log?
for f in *.txt; do echo "$f"; done
Aa.txt b.txt c.log
Ba.txt b.txt
C*.txt
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand the pattern '*.txt'

    The pattern '*.txt' matches files ending with '.txt' only, so 'a.txt' and 'b.txt' match, 'c.log' does not.
  2. Step 2: Loop prints each matched file

    The loop echoes each matched file name on a new line.
  3. Final Answer:

    a.txt b.txt -> Option B
  4. Quick Check:

    Pattern '*.txt' matches only .txt files = B [OK]
Quick Trick: Globs match only files fitting pattern [OK]
Common Mistakes:
MISTAKES
  • Expecting unmatched files to print
  • Thinking pattern prints literal '*.txt'
  • Assuming no files match

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes