Default values for input in Bash Scripting - Time & Space Complexity
When a script uses default values for input, it often checks if input is given or not.
We want to see how this check affects the script's running time as input size changes.
Analyze the time complexity of the following code snippet.
#!/bin/bash
input=${1:-default}
for i in $(seq 1 5); do
echo "Processing $input - step $i"
done
This script sets a default value if no input is given, then runs a loop 5 times printing a message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs 5 times.
- How many times: Exactly 5 times, fixed regardless of input.
The script runs the same 5-step loop no matter what input is given or if default is used.
| Input Size (n) | Approx. Operations |
|---|---|
| Empty or small input | 5 |
| Long input string | 5 |
| Very large input string | 5 |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the script runs in constant time, unaffected by input size.
[X] Wrong: "Using default values makes the script slower as input grows."
[OK] Correct: The default value check is a simple assignment and does not loop over input, so it does not slow down with bigger input.
Understanding how default inputs affect script speed helps you write reliable scripts that behave predictably no matter what input they get.
"What if the loop count depended on the length of the input string? How would the time complexity change?"
Practice
${name:-Guest} do in a bash script?Solution
Step 1: Understand the syntax ${variable:-default}
This syntax means: use the value ofvariableif it is set and not empty; otherwise, usedefault.Step 2: Apply to ${name:-Guest}
Ifnameis set and not empty, it returnsname. Ifnameis unset or empty, it returns 'Guest'.Final Answer:
It uses the value ofnameif set; otherwise, it uses 'Guest' as default. -> Option DQuick Check:
Default value syntax = It uses the value ofnameif set; otherwise, it uses 'Guest' as default. [OK]
- Thinking it assigns the default permanently
- Confusing with ${var:=default} which assigns default
- Assuming it throws error if variable unset
user with a default value 'admin' if input is empty?Solution
Step 1: Understand how to read input and set default
Thereadcommand takes input intouser. To set a default if empty, useuser=${user:-admin}.Step 2: Check each option
read user; user=${user:-admin} correctly reads input then sets default if empty. Options A, C, D use invalid syntax for default assignment.Final Answer:
read user; user=${user:-admin} -> Option AQuick Check:
Correct default syntax after read = read user; user=${user:-admin} [OK]
- Using || or && incorrectly for default assignment
- Trying to assign default inside read command
- Using invalid operators like ? for default
read -p "Enter your city: " city
city=${city:-Unknown}
echo "City: $city"Solution
Step 1: Analyze read input behavior
User presses Enter without input, socityis empty string.Step 2: Apply default value substitution
city=${city:-Unknown}setscityto 'Unknown' becausecityis empty.Final Answer:
City: Unknown -> Option CQuick Check:
Empty input triggers default = City: Unknown [OK]
- Expecting empty output instead of default
- Confusing ${var:-default} with ${var-default}
- Assuming error occurs on empty input
read name
name=${name=John}
echo "Name: $name"Solution
Step 1: Check default value syntax
The script uses${name=John}which is invalid for default substitution. The correct syntax is${name:-John}.Step 2: Confirm other parts
readwithout prompt is valid,nameis a valid variable, andechosyntax is correct.Final Answer:
The default value syntax is incorrect; should use :- instead of = -> Option BQuick Check:
Default syntax requires :- not = [OK]
- Using = instead of :- for default values
- Thinking read needs a prompt
- Confusing variable names with reserved words
Solution
Step 1: Check default value syntax for both variables
read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" uses${var:-default}which correctly sets default if variable is empty or unset.Step 2: Verify prompt usage and output
read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" usesread -pto prompt user, then sets defaults, then echoes both variables correctly.Final Answer:
read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" -> Option AQuick Check:
Use ${var:-default} with read -p for defaults = read -p "Username: " user user=${user:-guest} read -p "Port: " port port=${port:-8080} echo "User=$user, Port=$port" [OK]
- Using = instead of :- for defaults
- Not prompting user with read -p
- Using ${var-default} which skips empty but not unset
Ready to prove it?
Pass the test and this pattern moves unknown
