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?
hard
A. for f in "${files[@]}"; do echo ${f%.csv}; done
B. for f in "${files[@]}"; do echo ${f%%csv}; done
C. for f in "${files[@]}"; do echo ${f%csv}; done
D. for f in "${files[@]}"; do echo ${f#%.csv}; done
Solution
Step 1: Understand the goal
Remove '.csv' suffix only if it exists, leave other filenames unchanged.
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.
Step 3: Choose best option
for f in "${files[@]}"; do echo ${f%.csv}; done is correct and simplest for shortest suffix removal of '.csv'.
Final Answer:
for f in "${files[@]}"; do echo ${f%.csv}; done -> Option A
Quick Check:
Use ${var%.csv} to remove '.csv' suffix if present [OK]
Hint: Use ${var%.ext} in loop to trim suffix conditionally [OK]
Common Mistakes:
Using prefix removal (#) instead of suffix removal (%)
Omitting the dot in suffix pattern
Using %% unnecessarily when % suffices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.