Bird
0
0

In bash, what happens when you use the expression ${input:-default_value} inside a script?

easy🧠 Conceptual Q1 of 15
Bash Scripting - User Input
In bash, what happens when you use the expression ${input:-default_value} inside a script?
AIt returns <code>input</code> if set and non-empty; otherwise, it returns <code>default_value</code> without changing <code>input</code>.
BIt assigns <code>default_value</code> to <code>input</code> if <code>input</code> is unset or empty.
CIt always assigns <code>default_value</code> to <code>input</code> regardless of its current value.
DIt throws an error if <code>input</code> is unset.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the syntax

    The expression ${var:-default} returns the value of var if it is set and non-empty.
  2. Step 2: Behavior when var is unset or empty

    If var is unset or empty, it returns default but does NOT assign it to var.
  3. Final Answer:

    It returns input if set and non-empty; otherwise, it returns default_value without changing input. -> Option A
  4. Quick Check:

    Test with unset input and echo ${input:-default_value} [OK]
Quick Trick: :- returns default without assignment [OK]
Common Mistakes:
MISTAKES
  • Confusing :- with := which assigns default
  • Assuming default is assigned to variable
  • Thinking 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