Bird
0
0

What will be the output of this script?

medium📝 Command Output Q4 of 15
Bash Scripting - String Operations
What will be the output of this script?
unset user
user=""
echo "Current user: ${user:-guest}"
ACurrent user: guest
BCurrent user:
CCurrent user: user
DCurrent user: ${user:-guest}
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable state

    user is unset then set to an empty string.
  2. Step 2: Apply default value logic

    ${user:-guest} returns guest only if user is unset or null. Since user is set to empty string (which is set but null), it returns empty string.
  3. Final Answer:

    Current user: -> Option B
  4. Quick Check:

    Empty string is treated as set, so default is not used [OK]
Quick Trick: Empty string is treated as set, so default is not used [OK]
Common Mistakes:
MISTAKES
  • Expecting default value when variable is empty string
  • Confusing unset and empty string behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes