Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Quantifiers (*, +, ?) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Quantifiers (*, +, ?)
Start
↓
Input String
↓
Apply Regex with Quantifier
↓
Match Found?
No→No Match Output
Yes↓
Output Matched Portion
↓
End
The script takes an input string, applies a regex with a quantifier, checks for a match, and outputs the matched part or no match.
Execution Sample
Bash Scripting
echo "aaab" | grep -oE "^a*"
echo "aaab" | grep -oE "^a+"
echo "aaab" | grep -oE "^a?"
This code shows how the quantifiers *, +, and ? match parts of the string 'aaab' using grep.
Execution Table
StepCommandRegex QuantifierMatched OutputExplanation
1echo "aaab" | grep -oE "^a*""a*""aaa"Matches zero or more 'a's, finds 'aaa' at start
2echo "aaab" | grep -oE "^a+""a+""aaa"Matches one or more 'a's, finds 'aaa' at start
3echo "aaab" | grep -oE "^a?""a?""a"Matches zero or one 'a', finds 'a' at start
4echo "b" | grep -oE "^a*""a*"Matches zero or more 'a's at start, empty match (no output)
5echo "b" | grep -oE "^a+""a+"No match because 'a+' requires at least one 'a'
6echo "b" | grep -oE "^a?""a?"Matches zero or one 'a' at start, empty match (no output)
7EndAll commands executed
💡 All regex matches processed; some produce empty matches or no output depending on quantifier.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
Input String"aaab""aaab""aaab""aaab""b""b""b""b"
Regex Quantifier"a*""a+""a?""a*""a+""a?"
Matched Output"aaa""aaa""a"
Key Moments - 3 Insights
Why does the quantifier '*' match an empty string sometimes?
Because '*' means zero or more matches, it can match nothing. See step 4 and 6 where no output is produced (zero-length match not printed).
Why does '+' require at least one match and sometimes produce no output?
'+' means one or more matches. If no match is found, like in step 5, grep outputs nothing.
How is '?' different from '*' in matching?
'?' means zero or one match, so it matches either one character or empty. '*' can match many or none.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the matched output for 'a+' on input 'aaab'?
A"a"
B"aaa"
C"" (empty)
DNo output
💡 Hint
Check row 2 in the execution_table for 'a+' quantifier on 'aaab'.
At which step does the regex with '*' quantifier match an empty string?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Look at the matched output column for '*' quantifier on input 'b' in the execution_table.
If the input string changes to 'bbb', what would be the output for 'a+' quantifier?
A"aaa"
B"a"
CNo output
D"" (empty)
💡 Hint
Refer to step 5 where 'a+' on 'b' produces no output; more 'b's won't change that.
Concept Snapshot
Quantifiers in regex:
'*' = zero or more matches (can match empty)
'+' = one or more matches (must match at least once)
'?' = zero or one match
Use grep -oE to see matched parts
Zero-length matches produce no output
Full Transcript
This visual trace shows how regex quantifiers '*', '+', and '?' work in bash scripting using grep. The input string is tested against each quantifier. '*' matches zero or more characters, so it can match empty strings. '+' requires at least one match, so no match means no output. '?' matches zero or one character, so it can match empty or one character. The execution table shows each step's command, quantifier, matched output, and explanation. Variable tracker shows how input, quantifier, and matched output change. Key moments clarify common confusions about empty matches and quantifier differences. The quiz tests understanding by referencing the execution table and variable tracker.

Practice

(1/5)
1. In bash scripting, what does the quantifier * mean when used in a regular expression?
easy
A. Match zero or more occurrences of the preceding element
B. Match exactly one occurrence of the preceding element
C. Match one or more occurrences of the preceding element
D. Match zero or one occurrence of the preceding element

Solution

  1. Step 1: Understand the meaning of * in regex

    The * quantifier matches zero or more occurrences of the character or group before it.
  2. Step 2: Compare with other quantifiers

    Unlike + which requires at least one match, * allows zero matches too.
  3. Final Answer:

    Match zero or more occurrences of the preceding element -> Option A
  4. Quick Check:

    * means zero or more matches [OK]
Hint: Remember * means zero or more times, including none [OK]
Common Mistakes:
  • Confusing * with + which requires at least one match
  • Thinking * matches exactly one occurrence
  • Mixing * with ? which means zero or one
2. Which of the following is the correct way to use the + quantifier in grep to match one or more digits?
easy
A. grep '[0-9]+' file.txt
B. grep -P '[0-9]*' file.txt
C. grep '[0-9]*' file.txt
D. grep -E '[0-9]+' file.txt

Solution

  1. Step 1: Recognize that + needs extended regex

    The + quantifier is not supported by default in basic grep; it requires -E or -P option.
  2. Step 2: Identify correct grep syntax

    Using grep -E '[0-9]+' enables extended regex and matches one or more digits.
  3. Final Answer:

    grep -E '[0-9]+' file.txt -> Option D
  4. Quick Check:

    Use -E or -P for + quantifier [OK]
Hint: Use grep -E or -P to enable + quantifier [OK]
Common Mistakes:
  • Using + without -E or -P causes literal + match
  • Confusing * with + quantifier
  • Using -P without knowing if Perl regex is supported
3. What will be the output of the following command if file.txt contains the lines:
apple
apples
ap
ape

Command:
grep -E 'ap?le' file.txt
medium
A. apple
B. (no output)
C. apple aple
D. apple aple ape

Solution

  1. Step 1: Understand the regex ap?le

    The p? means zero or one 'p' character. So it matches 'ale' or 'aple'.
  2. Step 2: Check which lines match

    Lines: 'apple' (has two p's, so no), 'apples' (extra chars, no), 'ap' (too short), 'ape' (does not match 'ap?le' because 'l' is missing). Actually 'apple' has two 'p's, so 'ap?le' matches 'ale' or 'aple' but 'apple' has 'pp', so no match. 'ape' does not match 'ap?le' because 'l' is missing. So only 'apple' matches partially but not fully.
  3. Step 3: Re-examine lines carefully

    Actually, 'apple' is 'a p p l e'. The regex 'ap?le' means 'a' followed by zero or one 'p', then 'l', then 'e'. So 'apple' has two 'p's, so it does not match. 'ape' does not match 'ap?le' because 'l' is missing. So no output.
  4. Final Answer:

    apple -> Option A
  5. Quick Check:

    Regex matches zero or one p, so 'apple' no match [OK]
Hint: p? means zero or one p, so two p's won't match [OK]
Common Mistakes:
  • Assuming 'apple' matches when it has two p's
  • Ignoring that ? allows zero or one occurrence only
  • Confusing 'ap?le' with 'ap*le'
4. You wrote this grep command to find lines with one or more digits:
grep '[0-9]+' file.txt

But it returns no matches even though the file has digits. What is the likely problem?
medium
A. The regex should use * instead of +
B. The + quantifier needs -E or -P option to work
C. Digits are not matched by [0-9]
D. The file.txt is empty

Solution

  1. Step 1: Understand grep default regex

    By default, grep uses basic regex where + is treated as a literal plus sign, not a quantifier.
  2. Step 2: Identify correct usage for + quantifier

    To use + as 'one or more', grep needs -E (extended regex) or -P (Perl regex) option.
  3. Final Answer:

    The + quantifier needs -E or -P option to work -> Option B
  4. Quick Check:

    Use -E or -P for + quantifier [OK]
Hint: Add -E or -P to grep when using + quantifier [OK]
Common Mistakes:
  • Using + without enabling extended regex
  • Thinking * and + are interchangeable always
  • Assuming [0-9] does not match digits
5. You want to write a bash script that uses grep to find lines containing words that start with 'cat' followed by zero or one 's' (to match 'cat' or 'cats'). Which grep command correctly uses quantifiers to do this?
hard
A. grep -E '\bcat?s\b' file.txt
B. grep '\bcat?s\b' file.txt
C. grep -E '\bcats?\b' file.txt
D. grep '\bcats?\b' file.txt

Solution

  1. Step 1: Understand the pattern needed

    We want to match 'cat' or 'cats'. The 's' is optional, so use s? quantifier.
  2. Step 2: Use word boundaries and extended regex

    Use \b for word boundaries and -E to enable ? quantifier.
  3. Step 3: Check options

    grep -E '\bcats?\b' file.txt uses grep -E '\bcats?\b' which matches 'cat' or 'cats' correctly. Others either lack -E or misplace the quantifier.
  4. Final Answer:

    grep -E '\bcats?\b' file.txt -> Option C
  5. Quick Check:

    Use -E and s? to match zero or one s [OK]
Hint: Use -E and s? to match optional s after cat [OK]
Common Mistakes:
  • Forgetting -E option for ? quantifier
  • Placing ? after wrong character
  • Not using word boundaries causing partial matches
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