What if your script never broke because of missing input? Discover how a tiny trick fixes that!
Why Default values (${var:-default}) in Bash Scripting? - Purpose & Use Cases
Imagine you have a script that asks users for their name. Sometimes they forget to type it, and your script crashes or shows a blank message.
Manually checking if the name is empty means writing extra lines of code. It slows you down and can cause mistakes if you forget to check every time.
Using ${var:-default} lets you give a fallback value instantly. If the user skips input, your script uses the default without extra checks.
if [ -z "$name" ]; then name="Guest" fi
echo "Hello, ${name:-Guest}!"
This lets your scripts handle missing inputs smoothly and keep running without errors.
A script that sends emails can use a default sender address if none is provided, avoiding failed sends.
Default values prevent errors from missing inputs.
They reduce extra code and checks.
They make scripts more user-friendly and reliable.
Practice
${var:-default} do in a bash script?Solution
Step 1: Understand the syntax of
This syntax means: use${var:-default}varif it is set and not empty; otherwise, usedefault.Step 2: Check if
This expression does not assign or changevaris changedvar, it only provides a fallback value for use.Final Answer:
It uses the value ofvarif set; otherwise, it usesdefaultwithout changingvar. -> Option AQuick Check:
Default value used only ifvaris empty or unset [OK]
- Thinking it changes the variable's value
- Confusing :- with := which assigns default
- Assuming it throws an error if unset
name or use "Guest" if name is empty or unset?Solution
Step 1: Identify the correct default value syntax
The syntax${var:-default}printsvarif set; otherwise, printsdefault.Step 2: Check each option
echo ${name=Guest} assigns default if unset, not just prints. echo ${name+Guest} printsGuestonly ifnameis set. echo ${name?Guest} throws error ifnameis unset. Only echo ${name:-Guest} printsnameor "Guest" safely.Final Answer:
echo ${name:-Guest} -> Option DQuick Check:
Use :- for default fallback [OK]
- Using = which assigns instead of fallback
- Using + which prints default only if var is set
- Using ? which causes error if var unset
unset user
user=""
echo "User: ${user:-anonymous}"Solution
Step 1: Analyze variable state
The variableuseris first unset, then set to an empty string.Step 2: Apply default value logic
Sinceuseris empty (empty string counts as unset for :-), the expression uses the default "anonymous".Final Answer:
User: anonymous -> Option AQuick Check:
Empty variable triggers default value [OK]
- Thinking empty string is treated as set
- Expecting empty output instead of default
- Confusing unset and empty behavior
echo "Name: ${name-default}"What is the problem?
Solution
Step 1: Understand the difference between - and :-
Using${name-default}uses default only ifnameis unset, but not if empty.Step 2: Correct syntax for empty or unset
To cover both unset and empty,${name:-default}is needed.Final Answer:
It should be ${name:-default} to use default if name is empty or unset. -> Option CQuick Check:
Use :- to cover empty and unset [OK]
- Using - which ignores empty variables
- Confusing + and - operators
- Assuming syntax is always correct
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?Solution
Step 1: Understand the requirement
We want to printpathif it is set and not empty; otherwise, print the default "/usr/local/bin".Step 2: Choose correct parameter expansion
${path:-default}uses default ifpathis unset or empty.${path-default}only uses default if unset, not empty.${path:=default}assigns default topath.${path:+default}prints default only ifpathis set.Final Answer:
echo ${path:-/usr/local/bin} -> Option BQuick Check:
:- covers unset and empty [OK]
- Using - which ignores empty strings
- Using := which changes variable value
- Using + which prints default only if set
Ready to prove it?
Pass the test and this pattern moves unknown
