if-then-else in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to run an if-then-else statement changes as the input changes.
Specifically, does adding more data make it slower?
Analyze the time complexity of the following code snippet.
#!/bin/bash
value=$1
if [ "$value" -gt 10 ]; then
echo "Value is greater than 10"
else
echo "Value is 10 or less"
fi
This script checks if a number is greater than 10 and prints a message accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single if-then-else condition check
- How many times: Exactly once per script run
The script runs the if-then-else check once no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same regardless of input size.
Time Complexity: O(1)
This means the time to run the if-then-else does not grow with input size; it stays constant.
[X] Wrong: "If the input number is bigger, the script takes longer because it has to compare more digits."
[OK] Correct: The if-then-else only checks the value once, no matter how big the number is. It does not look at each digit separately.
Understanding that simple condition checks run in constant time helps you explain how your scripts behave as inputs change. This clear thinking is useful in many scripting and automation tasks.
"What if we added a loop inside the if-then-else that runs n times? How would the time complexity change?"
Practice
if-then-else structure do in a bash script?Solution
Step 1: Understand the purpose of if-then-else
The if-then-else structure checks a condition and runs code based on whether the condition is true or false.Step 2: Compare with other options
Repeating commands is done with loops, functions reuse code, and comments do not execute code.Final Answer:
It lets the script choose between two paths based on a condition. -> Option DQuick Check:
if-then-else = choose path [OK]
- Confusing if-then-else with loops
- Thinking if-then-else defines functions
- Mixing comments with control flow
Solution
Step 1: Recall bash if syntax
Bash requires spaces around brackets and a semicolon before then:if [ condition ]; then.Step 2: Check other options
if(condition) then misses spaces and semicolon, B uses braces incorrectly, D misses brackets and semicolon.Final Answer:
if [ condition ]; then -> Option AQuick Check:
Spaces and semicolon needed [OK]
- No spaces around brackets
- Missing semicolon before then
- Using parentheses or braces incorrectly
#!/bin/bash x=5 if [ $x -gt 3 ]; then echo "Greater" else echo "Smaller" fi
Solution
Step 1: Evaluate the condition
The condition checks if x (5) is greater than 3, which is true.Step 2: Determine which branch runs
Since condition is true, the script prints "Greater".Final Answer:
Greater -> Option CQuick Check:
5 > 3 = Greater [OK]
- Confusing -gt with -lt
- Missing spaces causing syntax errors
- Assuming else runs when condition is true
#!/bin/bash if [ $num -eq 10 ] echo "Ten" else echo "Not Ten" fi
Solution
Step 1: Check if statement syntax
The if condition must be followed bythenkeyword to start the true block.Step 2: Identify missing part
The script missesthenafter the condition line.Final Answer:
Missing then after condition -> Option AQuick Check:
if needs then [OK]
- Forgetting then keyword
- Using wrong comparison operators
- Missing fi to close if
num=4 # Choose correct script
Solution
Step 1: Understand arithmetic evaluation in bash
Using double parentheses (( )) allows arithmetic expressions and comparisons directly.Step 2: Check each option
if [ $num % 2 -eq 0 ]; then echo "Even" else echo "Odd" fi uses % inside [ ] without arithmetic evaluation. if [ $num % 2 == 0 ]; then echo "Even" else echo "Odd" fi uses % inside [ ] without arithmetic evaluation. if [ $num / 2 -eq 0 ]; then echo "Even" else echo "Odd" fi uses / instead of % for modulus. if (( num % 2 == 0 )); then echo "Even" else echo "Odd" fi correctly uses (( )) with modulus and comparison.Final Answer:
if (( num % 2 == 0 )); then echo "Even" else echo "Odd" fi -> Option BQuick Check:
Use (( )) for arithmetic if [OK]
- Using [ ] with arithmetic operators incorrectly
- Confusing / with % for modulus
- Missing then keyword
Ready to prove it?
Pass the test and this pattern moves unknown
