Bird
0
0

What will be the output of this script?

medium📝 Command Output Q5 of 15
Bash Scripting - Conditionals
What will be the output of this script?
str="hello"
if [[ $str == h* ]]; then
echo "Starts with h"
else
echo "Does not start with h"
fi
ASyntax error
BStarts with h
CDoes not start with h
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand pattern matching in [[ ]]

    Inside [[ ]], == supports glob patterns like h* which matches strings starting with 'h'.
  2. Step 2: Check if "hello" matches h*

    "hello" starts with 'h', so condition is true and "Starts with h" is printed.
  3. Final Answer:

    Starts with h -> Option B
  4. Quick Check:

    Pattern match with * works in [[ ]] = C [OK]
Quick Trick: Use glob patterns with == inside [[ ]] for string matching [OK]
Common Mistakes:
MISTAKES
  • Expecting regex instead of glob patterns
  • Using single [ ] which does not support glob
  • Quoting pattern which disables glob

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes