Bird
0
0

Given the list:

hard🚀 Application Q8 of 15
Bash Scripting - Loops
Given the list:
items="car bike bus boat"
for item in $items; do
  # condition here
  echo $item
done

Which condition will print only items starting with 'b'?
Aif [ $item == 'b*' ]; then
Bif [ $item = 'b' ]; then
Cif [[ $item =~ ^b$ ]]; then
Dif [[ $item == b* ]]; then
Step-by-Step Solution
Solution:
  1. Step 1: Understand pattern matching

    In Bash, [[ $var == b* ]] matches strings starting with 'b'.
  2. Step 2: Evaluate options

    if [[ $item == b* ]]; then correctly uses pattern matching; others are incorrect or match exact strings.
  3. Final Answer:

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

    Use double brackets and wildcard * for prefix matching [OK]
Quick Trick: Use [[ $var == prefix* ]] to match strings starting with prefix [OK]
Common Mistakes:
MISTAKES
  • Using single brackets with wildcard
  • Matching exact string instead of prefix
  • Using regex anchors incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes