Bird
0
0

What does the expression ${name:-Guest} do in a bash script?

easy🧠 Conceptual Q11 of 15
Bash Scripting - User Input
What does the expression ${name:-Guest} do in a bash script?
AIt throws an error if <code>name</code> is not set.
BIt assigns 'Guest' to <code>name</code> permanently.
CIt deletes the variable <code>name</code> if it is empty.
DIt uses the value of <code>name</code> if set; otherwise, it uses 'Guest' as default.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the syntax ${variable:-default}

    This syntax means: use the value of variable if it is set and not empty; otherwise, use default.
  2. Step 2: Apply to ${name:-Guest}

    If name is set and not empty, it returns name. If name is unset or empty, it returns 'Guest'.
  3. Final Answer:

    It uses the value of name if set; otherwise, it uses 'Guest' as default. -> Option D
  4. Quick Check:

    Default value syntax = It uses the value of name if set; otherwise, it uses 'Guest' as default. [OK]
Quick Trick: Use ${var:-default} to provide fallback values [OK]
Common Mistakes:
MISTAKES
  • Thinking it assigns the default permanently
  • Confusing with ${var:=default} which assigns default
  • Assuming it throws error if variable unset

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes