Bird
0
0

Identify the error in this bash snippet:

medium📝 Debug Q6 of 15
Bash Scripting - String Operations
Identify the error in this bash snippet:
name="John Doe"
echo ${name:3-2}
AInvalid substring syntax with arithmetic inside braces
BVariable name is incorrect
CMissing quotes around variable
DNo error, outputs 'n Doe'
Step-by-Step Solution
Solution:
  1. Step 1: Check substring syntax

    Substring syntax ${var:start:length} does not support arithmetic like 3-2 inside braces.
  2. Step 2: Understand bash does not evaluate expressions inside braces

    Arithmetic must be done outside or using $(( )) syntax.
  3. Final Answer:

    Invalid substring syntax with arithmetic inside braces -> Option A
  4. Quick Check:

    Arithmetic inside ${} invalid [OK]
Quick Trick: Do math outside ${} using $(( )) in bash [OK]
Common Mistakes:
MISTAKES
  • Trying to do math inside substring braces
  • Assuming quotes fix syntax errors
  • Thinking variable name is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes