Which expression sets a default value for a variable if it is unset or empty?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

Which expression sets a default value for a variable if it is unset or empty?

Explanation:
Bash parameter expansion for defaults is being tested. The form ${var1:=value} checks if var1 is unset or empty; if so, it assigns value to var1 and uses that value for the expansion. If var1 already has a non-empty value, it leaves it as is and uses the existing value. This makes it ideal for establishing a default when a variable hasn’t been set or is empty. For example, if var1 is unset, the expansion becomes value and var1 becomes value. If var1 already contains something, that existing value is used and no assignment occurs. The other forms don’t provide the same behavior: one assigns only if the variable is unset (not if it’s empty), another is just a static value, and the last only expands to value if var1 is set and non-empty but doesn’t create a default.

Bash parameter expansion for defaults is being tested. The form ${var1:=value} checks if var1 is unset or empty; if so, it assigns value to var1 and uses that value for the expansion. If var1 already has a non-empty value, it leaves it as is and uses the existing value. This makes it ideal for establishing a default when a variable hasn’t been set or is empty.

For example, if var1 is unset, the expansion becomes value and var1 becomes value. If var1 already contains something, that existing value is used and no assignment occurs. The other forms don’t provide the same behavior: one assigns only if the variable is unset (not if it’s empty), another is just a static value, and the last only expands to value if var1 is set and non-empty but doesn’t create a default.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy