Bird
0
0

You have a list of filenames:

hard🚀 Application Q15 of 15
Bash Scripting - String Operations
You have a list of filenames:
files=("data1.csv" "data2.csv" "notes.txt" "summary.csv")

You want to remove the '.csv' suffix only from files that end with '.csv' using Bash parameter expansion. Which command correctly prints the filenames with '.csv' removed where applicable?
Afor f in "${files[@]}"; do echo ${f%.csv}; done
Bfor f in "${files[@]}"; do echo ${f%%csv}; done
Cfor f in "${files[@]}"; do echo ${f%csv}; done
Dfor f in "${files[@]}"; do echo ${f#%.csv}; done
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    Remove '.csv' suffix only if it exists, leave other filenames unchanged.
  2. Step 2: Check each option's effect

    A uses single % with '.csv' pattern, removing suffix if present. B uses %%csv missing the dot, incorrect. C misses the dot, incorrect. D uses # which removes prefix, wrong direction.
  3. Step 3: Choose best option

    for f in "${files[@]}"; do echo ${f%.csv}; done is correct and simplest for shortest suffix removal of '.csv'.
  4. Final Answer:

    for f in "${files[@]}"; do echo ${f%.csv}; done -> Option A
  5. Quick Check:

    Use ${var%.csv} to remove '.csv' suffix if present [OK]
Quick Trick: Use ${var%.ext} in loop to trim suffix conditionally [OK]
Common Mistakes:
MISTAKES
  • Using prefix removal (#) instead of suffix removal (%)
  • Omitting the dot in suffix pattern
  • Using %% unnecessarily when % suffices

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes