Uppercase and lowercase conversion in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to change text case grows as the text gets longer.
How does the script's work increase when the input text size increases?
Analyze the time complexity of the following code snippet.
#!/bin/bash
input="$1"
# Convert to uppercase
uppercase=$(echo "$input" | tr '[:lower:]' '[:upper:]')
# Convert to lowercase
lowercase=$(echo "$input" | tr '[:upper:]' '[:lower:]')
echo "Uppercase: $uppercase"
echo "Lowercase: $lowercase"
This script takes a string input and converts it fully to uppercase and lowercase using the tr command.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
trcommand processes each character in the input string one by one. - How many times: It runs once for each character in the input string.
As the input string gets longer, the script must convert more characters, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 character conversions |
| 100 | About 200 character conversions |
| 1000 | About 2000 character conversions |
Pattern observation: The number of operations grows directly with the number of characters.
Time Complexity: O(n)
This means the time to convert case grows in a straight line with the input size.
[X] Wrong: "Changing case is instant and does not depend on input size."
[OK] Correct: Each character must be checked and changed, so longer text takes more time.
Understanding how simple text operations scale helps you explain script efficiency clearly and confidently.
"What if we used a command that converts only vowels to uppercase? How would the time complexity change?"