Default values (${var:-default}) in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to run a script changes when using default values in bash variables.
Specifically, how does checking and using a default value affect the script's speed as input grows?
Analyze the time complexity of the following code snippet.
for file in /some/directory/*; do
filename=${file:-default.txt}
echo "Processing $filename"
done
This script loops over files in a directory, assigns a default name if a variable is empty, and prints the name.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop iterates over each file name.
- How many times: Once for each file in the directory (n times).
- The default value check (${file:-default.txt}) happens inside the loop for each file.
Each file causes the loop to run once, doing a simple check and print.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and prints |
| 100 | About 100 checks and prints |
| 1000 | About 1000 checks and prints |
Pattern observation: The work grows directly with the number of files; doubling files doubles work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of files processed.
[X] Wrong: "Using a default value check inside the loop makes the script run slower exponentially."
[OK] Correct: The default check is a simple operation done once per item, so it only adds a small constant time each loop, not exponential growth.
Understanding how simple variable checks inside loops affect performance helps you write efficient scripts and explain your reasoning clearly in interviews.
"What if we replaced the default value check with a function call inside the loop? How would the time complexity change?"
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
