0
0
Bash Scriptingscripting

Quantifiers (*, +, ?) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the asterisk (*) quantifier mean in bash regular expressions?
The asterisk (*) means "zero or more" of the preceding character or group. It matches if the pattern appears any number of times, including not at all.
Click to reveal answer
beginner
Explain the plus (+) quantifier in bash regex.
The plus (+) means "one or more" of the preceding character or group. It requires at least one occurrence to match.
Click to reveal answer
beginner
What does the question mark (?) quantifier do in bash regex?
The question mark (?) means "zero or one" of the preceding character or group. It matches if the pattern appears once or not at all.
Click to reveal answer
intermediate
In bash scripting, how would you check if a variable contains one or more digits using regex?
You can use [[ $var =~ [0-9]+ ]] which means the variable contains one or more digits.
Click to reveal answer
beginner
True or False: The regex pattern 'a*' matches the string 'bbb' in bash.
True. 'a*' matches zero or more 'a's, so it matches even if there are no 'a's, like in 'bbb'.
Click to reveal answer
What does the regex quantifier '+' mean in bash?
AExactly one occurrence
BZero or more occurrences
CZero or one occurrence
DOne or more occurrences
Which quantifier matches zero or more times in bash regex?
A?
B*
C+
D{1,}
What does the '?' quantifier do in bash regex?
AMatches one or more times
BMatches zero or more times
CMatches zero or one time
DMatches exactly two times
Which regex pattern matches a string with zero or more 'a's?
Aa*
Ba?
Ca+
Da{1,}
In bash, which regex would match a string with at least one digit?
A[0-9]+
B[0-9]*
C[0-9]?
D[0-9]{0}
Describe the difference between the '*', '+', and '?' quantifiers in bash regex.
Think about how many times the pattern can appear.
You got /3 concepts.
    How would you use a quantifier in bash to check if a variable contains an optional character?
    Optional means it can be there or not.
    You got /3 concepts.