Bird
0
0

You want to print only the fruits that start with the letter 'b' from this list:

hard🚀 Application Q15 of 15
Bash Scripting - Loops
You want to print only the fruits that start with the letter 'b' from this list:
fruits="apple banana cherry blueberry"
for fruit in $fruits; do
  # Fill condition here
  echo "$fruit"
done

Which condition inside the loop correctly filters fruits starting with 'b'?
Aif [ $fruit = 'b' ]; then
Bif [[ $fruit == b* ]]; then
Cif [[ $fruit =~ b* ]]; then
Dif [ $fruit == 'b*' ]; then
Step-by-Step Solution
Solution:
  1. Step 1: Understand string pattern matching in Bash

    Double brackets [[ ]] support pattern matching with == and wildcards like '*'.
  2. Step 2: Check each condition

    A uses [[ $fruit == b* ]] which matches strings starting with 'b'. B uses single brackets for exact 'b' match. C uses [[ $fruit =~ b* ]]; b* regex matches zero or more 'b's (including none), so every string. D uses single brackets which don't support patterns or ==.
  3. Final Answer:

    if [[ $fruit == b* ]]; then -> Option B
  4. Quick Check:

    Use [[ var == pattern* ]] for prefix match = A [OK]
Quick Trick: Use [[ var == prefix* ]] to match string start in Bash [OK]
Common Mistakes:
MISTAKES
  • Using single brackets with wildcard patterns
  • Confusing regex =~ with == operator
  • Comparing to exact string 'b' instead of prefix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes