Bird
0
0

Which of the following is the correct syntax to loop over all files with extension '.txt' in bash?

easy📝 Syntax Q3 of 15
Bash Scripting - Loops
Which of the following is the correct syntax to loop over all files with extension '.txt' in bash?
Afor file in *.txt; echo $file; done
Bfor file in *.txt; do echo $file; done
Cfor file *.txt; do echo $file; done
Dfor file in *.txt do echo $file; done
Step-by-Step Solution
Solution:
  1. Step 1: Check correct for loop syntax

    The correct syntax is 'for variable in list; do commands; done'. for file in *.txt; do echo $file; done follows this.
  2. Step 2: Verify pattern usage

    '*.txt' matches all files ending with .txt. for file in *.txt; do echo $file; done uses this correctly.
  3. Final Answer:

    for file in *.txt; do echo $file; done -> Option B
  4. Quick Check:

    Correct for loop syntax with pattern = A [OK]
Quick Trick: Use 'for var in pattern; do ... done' for loops [OK]
Common Mistakes:
MISTAKES
  • Missing 'in' keyword
  • Missing 'do' keyword
  • Incorrect loop variable declaration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes