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?"
Practice
name to all uppercase letters?Solution
Step 1: Understand bash variable expansion for case conversion
Bash supports${variable^^}to convert all letters invariableto uppercase.Step 2: Compare options with bash syntax
Using${name^^}uses${name^^}, which is the correct bash syntax. Using${name,,}converts to lowercase, and options C and D are not valid bash syntax.Final Answer:
Using ${name^^} -> Option CQuick Check:
Uppercase conversion = ${variable^^} [OK]
- Confusing uppercase and lowercase syntax
- Using non-bash functions like toUpperCase()
- Forgetting the double caret ^^
text to lowercase in bash?Solution
Step 1: Recall lowercase conversion syntax in bash
To convert all letters to lowercase, bash uses${variable,,}.Step 2: Analyze each option
echo ${text,,}uses${text,,}which converts all letters to lowercase.echo ${text^^}converts to uppercase, C and D convert only the first letter.Final Answer:
echo ${text,,} -> Option AQuick Check:
Lowercase conversion = ${variable,,} [OK]
- Using single comma instead of double for all letters
- Mixing uppercase and lowercase syntax
- Confusing ^ and , operators
word="HelloWorld"
echo ${word,,}Solution
Step 1: Understand the variable and conversion
The variablewordcontains "HelloWorld". The expression${word,,}converts all letters to lowercase.Step 2: Apply lowercase conversion
Converting "HelloWorld" to lowercase results in "helloworld".Final Answer:
helloworld -> Option AQuick Check:
Lowercase conversion of HelloWorld = helloworld [OK]
- Expecting only first letter to change
- Confusing uppercase and lowercase syntax
- Ignoring case conversion operator
input to uppercase, but it does not work as expected:input="bashScript" echo $input^^
What is the error?
Solution
Step 1: Identify correct syntax for uppercase conversion
Bash requires braces around the variable when using case conversion, like${input^^}.Step 2: Analyze the script
The script uses$input^^without braces, which is invalid syntax and will not convert case.Final Answer:
Missing braces around the variable for case conversion -> Option BQuick Check:
Use ${variable^^} with braces for uppercase [OK]
- Omitting braces around variable
- Using single caret ^ for all letters
- Assuming bash lacks uppercase conversion
files=("Report1.txt" "summary2.TXT" "DATA3.Txt"). You want to convert all filenames to lowercase and print them line by line. Which script correctly does this?Solution
Step 1: Understand the goal and array iteration
The goal is to convert each filename to lowercase and print it. Iterating over"${files[@]}"correctly accesses each element.Step 2: Check each option's correctness
for f in "${files[@]}"; do echo ${f,,}; done uses${f,,}to convert to lowercase and iterates correctly. for f in "${files[@]}"; do echo ${f^^}; done converts to uppercase, C iterates incorrectly (missing quotes and array expansion), D uses a non-bash functiontolower().Final Answer:
for f in "${files[@]}"; do echo ${f,,}; done -> Option DQuick Check:
Lowercase array elements with ${var,,} in loop [OK]
- Using uppercase conversion instead of lowercase
- Incorrect array iteration syntax
- Using non-existent bash functions
Ready to prove it?
Pass the test and this pattern moves unknown
