Recall & Review
beginner
What does the expression
${var:-default} do in bash scripting?It checks if the variable
var is set and not empty. If var is unset or empty, it uses the default value instead.Click to reveal answer
intermediate
How does
${var:-default} differ from ${var-default} in bash?${var:-default} uses the default if var is unset or empty, while ${var-default} uses the default only if var is unset (empty is allowed).Click to reveal answer
beginner
Write a bash command using
${var:-default} to print the value of name or 'Guest' if name is empty or unset.echo "Hello, ${name:-Guest}!"
Click to reveal answer
intermediate
What happens if
var is set to an empty string and you use ${var:-default}?The expression treats
var as empty and returns default because :- checks for unset or empty.Click to reveal answer
intermediate
Can
${var:-default} change the value of var?No, it only returns the default value if
var is unset or empty but does not assign it to var. To assign, use ${var:=default}.Click to reveal answer
What will
echo ${user:-anonymous} print if user is unset?✗ Incorrect
Since
user is unset, ${user:-anonymous} returns the default 'anonymous'.If
var="" (empty string), what does echo ${var:-default} output?✗ Incorrect
:- treats empty as unset, so it outputs the default value.Which expression assigns the default value to
var if it is unset or empty?✗ Incorrect
${var:=default} assigns the default to var if unset or empty.What does
${var-default} do differently from ${var:-default}?✗ Incorrect
${var-default} uses default only if var is unset, but allows empty values.What will
echo ${var:-"No value"} print if var="Hello"?✗ Incorrect
Since
var is set and not empty, it prints its value 'Hello'.Explain how the bash expression
${var:-default} works and when it is useful.Think about how to avoid errors when variables might be empty.
You got /4 concepts.
Describe the difference between
${var:-default} and ${var:=default} in bash scripting.One changes the variable, the other just returns a value.
You got /4 concepts.