Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Default values (${var:-default}) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Default values (${var:-default})
Start
↓
Check if var is set and not empty
↓
Use var value
↓
Output
↓
End
The script checks if a variable is set and not empty; if yes, it uses its value, otherwise it uses the default value.
Execution Sample
Bash Scripting
name=""
echo "Hello, ${name:-Guest}!"

name="Alice"
echo "Hello, ${name:-Guest}!"
This code prints a greeting using the variable 'name' if set and not empty; otherwise, it uses 'Guest' as default.
Execution Table
StepVariable 'name'Condition '${name:-Guest}'Output
1'' (empty string)Empty or unset, use default 'Guest'Hello, Guest!
2'Alice'Set and not empty, use 'Alice'Hello, Alice!
💡 Execution ends after printing greetings with either the default or the variable value.
Variable Tracker
VariableStartAfter Step 1After Step 2
nameunset'' (empty string)'Alice'
Key Moments - 2 Insights
Why does the first echo print 'Guest' even though 'name' is set to an empty string?
Because '${name:-Guest}' treats empty strings as unset or empty, so it uses the default 'Guest' as shown in execution_table step 1.
What happens if 'name' is set to a non-empty value?
The expression uses the actual value of 'name', as in execution_table step 2 where 'Alice' is printed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when 'name' is an empty string?
AHello, Guest!
BHello, !
CHello, name!
DHello, ${name}!
💡 Hint
Check execution_table row 1 under Output column.
At which step does the condition '${name:-Guest}' use the variable's actual value?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
See execution_table Condition column for Step 2.
If 'name' was unset instead of empty, what would the output be?
AHello, name!
BHello, !
CHello, Guest!
DError
💡 Hint
Default value is used when variable is unset or empty, as shown in Step 1.
Concept Snapshot
Syntax: ${var:-default}
If 'var' is unset or empty, use 'default'.
Otherwise, use 'var' value.
Common in scripts to provide fallback values.
Example: echo "${name:-Guest}" prints 'Guest' if name is empty or unset.
Full Transcript
This visual execution shows how the bash syntax ${var:-default} works. First, it checks if the variable 'name' is set and not empty. If 'name' is empty or unset, it uses the default value 'Guest'. Otherwise, it uses the actual value of 'name'. The example runs two steps: first with 'name' as an empty string, printing 'Hello, Guest!'; second with 'name' set to 'Alice', printing 'Hello, Alice!'. This helps beginners see how default values provide safe fallbacks in scripts.

Practice

(1/5)
1. What does the expression ${var:-default} do in a bash script?
easy
A. It uses the value of var if set; otherwise, it uses default without changing var.
B. It sets var to default if var is unset or empty.
C. It always replaces var with default.
D. It throws an error if var is unset or empty.

Solution

  1. Step 1: Understand the syntax of ${var:-default}

    This syntax means: use var if it is set and not empty; otherwise, use default.
  2. Step 2: Check if var is changed

    This expression does not assign or change var, it only provides a fallback value for use.
  3. Final Answer:

    It uses the value of var if set; otherwise, it uses default without changing var. -> Option A
  4. Quick Check:

    Default value used only if var is empty or unset [OK]
Hint: Remember: :- means use default if empty or unset [OK]
Common Mistakes:
  • Thinking it changes the variable's value
  • Confusing :- with := which assigns default
  • Assuming it throws an error if unset
2. Which of the following is the correct syntax to print a variable name or use "Guest" if name is empty or unset?
easy
A. echo ${name=Guest}
B. echo ${name?Guest}
C. echo ${name+Guest}
D. echo ${name:-Guest}

Solution

  1. Step 1: Identify the correct default value syntax

    The syntax ${var:-default} prints var if set; otherwise, prints default.
  2. Step 2: Check each option

    echo ${name=Guest} assigns default if unset, not just prints. echo ${name+Guest} prints Guest only if name is set. echo ${name?Guest} throws error if name is unset. Only echo ${name:-Guest} prints name or "Guest" safely.
  3. Final Answer:

    echo ${name:-Guest} -> Option D
  4. Quick Check:

    Use :- for default fallback [OK]
Hint: Use :- to print default without assignment [OK]
Common Mistakes:
  • Using = which assigns instead of fallback
  • Using + which prints default only if var is set
  • Using ? which causes error if var unset
3. What is the output of this script?
unset user
user=""
echo "User: ${user:-anonymous}"
medium
A. User: anonymous
B. User:
C. User: user
D. User: ${user:-anonymous}

Solution

  1. Step 1: Analyze variable state

    The variable user is first unset, then set to an empty string.
  2. Step 2: Apply default value logic

    Since user is empty (empty string counts as unset for :-), the expression uses the default "anonymous".
  3. Final Answer:

    User: anonymous -> Option A
  4. Quick Check:

    Empty variable triggers default value [OK]
Hint: Empty or unset triggers default with :- [OK]
Common Mistakes:
  • Thinking empty string is treated as set
  • Expecting empty output instead of default
  • Confusing unset and empty behavior
4. Identify the error in this script snippet:
echo "Name: ${name-default}"

What is the problem?
medium
A. The dash should be a plus sign: ${name+default}.
B. The syntax is correct and will work as expected.
C. It should be ${name:-default} to use default if name is empty or unset.
D. The variable name is missing a $ sign.

Solution

  1. Step 1: Understand the difference between - and :-

    Using ${name-default} uses default only if name is unset, but not if empty.
  2. Step 2: Correct syntax for empty or unset

    To cover both unset and empty, ${name:-default} is needed.
  3. Final Answer:

    It should be ${name:-default} to use default if name is empty or unset. -> Option C
  4. Quick Check:

    Use :- to cover empty and unset [OK]
Hint: Use :- not - to cover empty and unset [OK]
Common Mistakes:
  • Using - which ignores empty variables
  • Confusing + and - operators
  • Assuming syntax is always correct
5. You want to print the value of path if set and not empty, otherwise print "/usr/local/bin". But if path is set to an empty string, you want to treat it as unset. Which command correctly does this?
hard
A. echo ${path-/usr/local/bin}
B. echo ${path:-/usr/local/bin}
C. echo ${path:=/usr/local/bin}
D. echo ${path:+/usr/local/bin}

Solution

  1. Step 1: Understand the requirement

    We want to print path if it is set and not empty; otherwise, print the default "/usr/local/bin".
  2. Step 2: Choose correct parameter expansion

    ${path:-default} uses default if path is unset or empty. ${path-default} only uses default if unset, not empty. ${path:=default} assigns default to path. ${path:+default} prints default only if path is set.
  3. Final Answer:

    echo ${path:-/usr/local/bin} -> Option B
  4. Quick Check:

    :- covers unset and empty [OK]
Hint: Use :- to treat empty as unset and provide default [OK]
Common Mistakes:
  • Using - which ignores empty strings
  • Using := which changes variable value
  • Using + which prints default only if set
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard