Bird
0
0

Why does this script fail in sh but work in Bash?

medium📝 Debug Q7 of 15
Bash Scripting - Basics
Why does this script fail in sh but work in Bash?
[[ -f file.txt ]] && echo "File exists"
Ash does not support && operator
B[[ ]] is a Bash-specific test syntax
CMissing quotes around file.txt
DFile does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Understand test command differences

    [[ ]] is a Bash extension; sh uses [ ] or test command.
  2. Step 2: Identify sh compatibility

    sh does not recognize [[ ]] syntax, causing failure.
  3. Final Answer:

    [[ ]] is a Bash-specific test syntax -> Option B
  4. Quick Check:

    [[ ]] unsupported in sh = Script fails [OK]
Quick Trick: Use [ ] for POSIX sh compatibility [OK]
Common Mistakes:
MISTAKES
  • Thinking && is unsupported in sh
  • Assuming quotes cause failure
  • Ignoring test syntax differences

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes