Character classes ([a-z], [0-9]) in Bash Scripting - Time & Space Complexity
We want to see how the time to run a bash script changes when it uses character classes like [a-z] or [0-9].
How does the script's work grow as the input size grows?
Analyze the time complexity of the following code snippet.
#!/bin/bash
input="$1"
count=0
for (( i=0; i<${#input}; i++ )); do
char=${input:i:1}
if [[ $char =~ [a-z] ]]; then
((count++))
fi
done
echo "Lowercase letters count: $count"
This script counts how many lowercase letters are in the input string using the [a-z] character class.
- Primary operation: Looping through each character of the input string.
- How many times: Once for every character in the input (length n).
Each character is checked once to see if it matches the [a-z] pattern.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The work grows directly with the input size. Double the input, double the checks.
Time Complexity: O(n)
This means the script takes time proportional to the length of the input string.
[X] Wrong: "Using character classes like [a-z] makes the script slower in a way that depends on the number of matches."
[OK] Correct: The script checks each character once regardless of matches, so time depends only on input length, not how many characters match.
Understanding how pattern matching scales helps you write efficient scripts and explain your reasoning clearly in interviews.
"What if we changed the script to check for multiple character classes inside the loop? How would the time complexity change?"
Practice
[a-z] match in bash scripting?Solution
Step 1: Understand character class notation
The notation[a-z]means any character from 'a' to 'z' in lowercase.Step 2: Identify what
This matches all lowercase letters, not uppercase letters, digits, or special characters.[a-z]matchesFinal Answer:
All lowercase letters from a to z -> Option AQuick Check:
[a-z]= lowercase letters [OK]
- Confusing lowercase with uppercase letters
- Thinking it matches digits
- Assuming it matches special characters
Solution
Step 1: Identify digit matching syntax
The character class[0-9]matches any digit from 0 to 9.Step 2: Check other options
[a-z]matches lowercase letters,[A-Z]matches uppercase letters, and[9-0]is invalid because ranges must go from lower to higher.Final Answer:
[0-9] -> Option DQuick Check:
[0-9]= digits [OK]
- Using reversed ranges like [9-0]
- Confusing letters with digits
- Using uppercase letter ranges for digits
echo -e "a1 b2 c3" | grep -o '[a-z][0-9]'
Solution
Step 1: Understand the grep command with -o and pattern
The pattern[a-z][0-9]matches a lowercase letter followed by a digit. The-ooption prints each match on a new line.Step 2: Apply pattern to input string
Input "a1 b2 c3" contains matches: "a1", "b2", and "c3". Each will be printed on its own line.Final Answer:
a1 b2 c3 -> Option BQuick Check:
Pattern matches pairs, -o prints each on new line [OK]
- Expecting all matches on one line
- Matching letters or digits alone
- Ignoring the -o option effect
grep '[a-z-9]' file.txt
Solution
Step 1: Analyze the character class syntax
The pattern[a-z-9]is intended to match letters and digits, but the dash is misplaced between 'z' and '9'.Step 2: Understand dash usage in character classes
Dash (-) defines a range if placed between two characters. Here, 'z-9' is invalid because 'z' comes after '9' in ASCII order. Dash should be first or last to be literal.Final Answer:
The character class is invalid because of the dash placement -> Option CQuick Check:
Dash in ranges must go from lower to higher [OK]
- Placing dash between characters in wrong order
- Confusing quotes usage
- Using wrong bracket types
grep -E '\b[?]+\b' file.txt
Choose the correct pattern to replace
?.Solution
Step 1: Understand the requirement for matching words
We want words with only lowercase letters and digits, any order, any length (at least one character).Step 2: Analyze each pattern
[a-z0-9]+matches one or more lowercase letters or digits in any order.[a-z]+[0-9]+requires letters followed by digits only.[a-z][0-9]matches exactly two characters: one letter then one digit.[0-9a-z]{2,}matches two or more letters/digits but order is not enforced; this is valid but less clear than [a-z0-9]+.Final Answer:
[a-z0-9]+ -> Option AQuick Check:
Character class with + matches any length words [OK]
- Forcing letters then digits order
- Matching only two characters
- Ignoring word boundaries
Ready to prove it?
Pass the test and this pattern moves unknown
