Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Character classes ([a-z], [0-9]) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Character Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of grep with character class [a-z]
What is the output of this command when run on a file containing the lines:

apple
Banana
cherry
123
date

grep '^[a-z]' filename

Assume the file is named filename and contains exactly the lines above.
Bash Scripting
grep '^[a-z]' filename
A
apple
Banana
cherry
date
B
apple
Banana
cherry
123
date
C
Banana
123
D
apple
cherry
date
Attempts:
2 left
💡 Hint
The pattern '^[a-z]' matches lines starting with a lowercase letter.
💻 Command Output
intermediate
2:00remaining
Matching digits with grep [0-9]
Given a file numbers.txt with the following lines:

abc123
456def
789
no_digits
123abc456

What lines will this command output?

grep '^[0-9]' numbers.txt
Bash Scripting
grep '^[0-9]' numbers.txt
A
456def
789
123abc456
B
abc123
456def
789
123abc456
C
789
no_digits
D
abc123
no_digits
Attempts:
2 left
💡 Hint
The pattern '^[0-9]' matches lines starting with a digit.
🔧 Debug
advanced
2:00remaining
Why does this grep command fail to match digits?
You run this command:

grep '[0-9]$' file.txt

But it does not match lines ending with digits as expected. What is the most likely reason?
Bash Scripting
grep '[0-9]$' file.txt
AThe character class [0-9] is invalid in grep.
BThe pattern is missing a caret ^ at the start.
CThe file has Windows line endings, so the $ matches before the carriage return, not the digit.
DThe file is empty, so no lines match.
Attempts:
2 left
💡 Hint
Check the file line endings if the pattern looks correct.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this character class usage
Which option contains a syntax error in the grep pattern using character classes?
Bash Scripting
grep '^[a-z0-9' file.txt
Agrep '^[a-z0-9' file.txt
Bgrep '^[a-z0-9-' file.txt
Cgrep '^[a-z0-9]' file.txt
Dtxt.elif ']9-0z-a[^' perg
Attempts:
2 left
💡 Hint
Check if the character class is properly closed with a bracket.
🚀 Application
expert
2:00remaining
Count lines starting with lowercase letters and digits
You want to count how many lines in data.txt start with either a lowercase letter or a digit. Which command will give the correct count?
Agrep -c '^[a-z]|^[0-9]' data.txt
Bgrep -c '^[a-z0-9]' data.txt
Cgrep -c '^[a-z][0-9]' data.txt
Dgrep -c '^[a-z0-9]$' data.txt
Attempts:
2 left
💡 Hint
Use a single character class to match either lowercase letters or digits at line start.

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

  1. Step 1: Understand character class notation

    The notation [a-z] means any character from 'a' to 'z' in lowercase.
  2. Step 2: Identify what [a-z] matches

    This matches all lowercase letters, not uppercase letters, digits, or special characters.
  3. Final Answer:

    All lowercase letters from a to z -> Option A
  4. 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

  1. Step 1: Identify digit matching syntax

    The character class [0-9] matches any digit from 0 to 9.
  2. 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.
  3. Final Answer:

    [0-9] -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    a1 b2 c3 -> Option B
  4. 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

  1. 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'.
  2. 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.
  3. Final Answer:

    The character class is invalid because of the dash placement -> Option C
  4. 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

  1. 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).
  2. 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]+.
  3. Final Answer:

    [a-z0-9]+ -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard