What does the character class [a-z] represent in bash scripting?
It matches any single lowercase letter from 'a' to 'z'. Think of it like choosing any letter from a small box labeled with all lowercase letters.
Click to reveal answer
beginner
How does [0-9] help in pattern matching?
It matches any single digit from 0 to 9. Imagine it as picking any number from 0 through 9 in a list.
Click to reveal answer
intermediate
What would the pattern [a-z0-9] match?
It matches any single lowercase letter or digit. Like choosing one item from a mixed box of letters and numbers.
Click to reveal answer
beginner
In bash scripting, how do character classes improve searching text?
They let you match groups of characters easily without listing each one. For example, <code>[a-z]</code> matches any lowercase letter, saving time and effort.
Click to reveal answer
beginner
True or False: The pattern [a-z] matches uppercase letters.
False. It only matches lowercase letters from 'a' to 'z'. Uppercase letters require [A-Z].
Click to reveal answer
What does the pattern [0-9] match in bash scripting?
AAny uppercase letter
BAny lowercase letter
CAny special character
DAny single digit from 0 to 9
✗ Incorrect
[0-9] matches any single digit between 0 and 9.
Which pattern matches any lowercase letter or digit?
A[a-zA-Z]
B[A-Z0-9]
C[a-z0-9]
D[0-9]
✗ Incorrect
[a-z0-9] matches any lowercase letter or digit.
Does [a-z] match the letter 'G'?
ANo
BYes
COnly if case insensitive
DOnly in some shells
✗ Incorrect
[a-z] matches only lowercase letters, so 'G' is not matched.
What is the benefit of using character classes like [a-z] in bash scripts?
AThey simplify matching groups of characters
BThey match only one specific character
CThey slow down the script
DThey are used to comment code
✗ Incorrect
Character classes simplify matching any one character from a set, making scripts easier to write.
Which pattern matches any uppercase letter?
A[a-z]
B[A-Z]
C[0-9]
D[a-z0-9]
✗ Incorrect
[A-Z] matches any uppercase letter from A to Z.
Explain what character classes like [a-z] and [0-9] do in bash scripting.
Think about how you pick letters or numbers from a group.
You got /4 concepts.
Describe a simple example where you would use [a-z0-9] in a bash script.
Imagine checking if a character is a letter or number.
You got /4 concepts.
Practice
(1/5)
1. What does the character class [a-z] match in bash scripting?
easy
A. All lowercase letters from a to z
B. All uppercase letters from A to Z
C. All digits from 0 to 9
D. All special characters
Solution
Step 1: Understand character class notation
The notation [a-z] means any character from 'a' to 'z' in lowercase.
Step 2: Identify what [a-z] matches
This matches all lowercase letters, not uppercase letters, digits, or special characters.
Final Answer:
All lowercase letters from a to z -> Option A
Quick Check:
[a-z] = lowercase letters [OK]
Hint: Remember: a-z means lowercase letters only [OK]
Common Mistakes:
Confusing lowercase with uppercase letters
Thinking it matches digits
Assuming it matches special characters
2. Which of the following is the correct syntax to match any digit using a character class in bash?
easy
A. [9-0]
B. [a-z]
C. [A-Z]
D. [0-9]
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 D
Quick Check:
[0-9] = digits [OK]
Hint: Digits always use [0-9], not reversed ranges [OK]
Common Mistakes:
Using reversed ranges like [9-0]
Confusing letters with digits
Using uppercase letter ranges for digits
3. What will be the output of this command?
echo -e "a1 b2 c3" | grep -o '[a-z][0-9]'
medium
A. a1 b2 c3
B. a1
b2
c3
C. a b c
D. 1 2 3
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 -o option 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 B
Quick Check:
Pattern matches pairs, -o prints each on new line [OK]
Hint: Use -o to print each match on a separate line [OK]
Common Mistakes:
Expecting all matches on one line
Matching letters or digits alone
Ignoring the -o option effect
4. Identify the error in this grep command:
grep '[a-z-9]' file.txt
medium
A. Should use double brackets [[ ]] instead of single brackets
B. Missing quotes around the pattern
C. The character class is invalid because of the dash placement
D. No error, command is correct
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 C
Quick Check:
Dash in ranges must go from lower to higher [OK]
Hint: Put dash at start or end to avoid range errors [OK]
Common Mistakes:
Placing dash between characters in wrong order
Confusing quotes usage
Using wrong bracket types
5. You want to extract all words from a file that contain only lowercase letters and digits. Which grep pattern correctly matches such words?
grep -E '\b[?]+\b' file.txt
Choose the correct pattern to replace ?.
hard
A. [a-z0-9]+
B. [a-z]+[0-9]+
C. [a-z][0-9]
D. [0-9a-z]{2,}
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 A
Quick Check:
Character class with + matches any length words [OK]
Hint: Use [a-z0-9]+ to match letters and digits in any order [OK]
Common Mistakes:
Forcing letters then digits order
Matching only two characters
Ignoring word boundaries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.