Bird
0
0

Which bash expression assigns a default value to a variable only if it is unset or empty?

easy🧠 Conceptual Q2 of 15
Bash Scripting - User Input
Which bash expression assigns a default value to a variable only if it is unset or empty?
Avar=${var:=default}
Bvar=${var:-default}
Cvar=${var-default}
Dvar=${var=default}
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between :- and :=

    ${var:-default} uses default if var is unset or empty but does not assign it. ${var:=default} assigns default to var if it is unset or empty.
  2. Step 2: Identify assignment syntax

    Only ${var:=default} assigns the default value to the variable permanently.
  3. Final Answer:

    var=${var:=default} -> Option A
  4. Quick Check:

    Assign default if unset = A [OK]
Quick Trick: Use := to assign default value if variable is empty or unset [OK]
Common Mistakes:
MISTAKES
  • Confusing :- with := and expecting assignment
  • Using single = inside braces which is invalid
  • Using - instead of := for assignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes