Bird
0
0

Why does this script print '$var' literally instead of the variable's value?

medium📝 Debug Q7 of 15
Bash Scripting - Quoting and Expansion
Why does this script print '$var' literally instead of the variable's value?
var="Hello World"
echo '$var'
AVariable name is invalid
BSingle quotes prevent variable expansion
CMissing semicolon after echo
DDouble quotes are required for echo
Step-by-Step Solution
Solution:
  1. Step 1: Understand single quotes effect

    Single quotes treat everything literally, so '$var' prints the string "$var" not its value.
  2. Step 2: Why this causes confusion

    User expects variable expansion but single quotes block it, so output is not as intended.
  3. Final Answer:

    Single quotes prevent variable expansion -> Option B
  4. Quick Check:

    Single quotes block expansion = B [OK]
Quick Trick: Use double quotes for variable expansion [OK]
Common Mistakes:
MISTAKES
  • Using single quotes expecting expansion
  • Assuming variable name error
  • Thinking echo needs semicolon

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes