0
0
Bash Scriptingscripting~10 mins

Capture groups in Bash in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Capture groups in Bash
Input string
Apply regex with capture groups
Match found?
NoNo capture groups
Yes
Extract groups into variables
Use captured values
Bash applies a regex with capture groups to a string, checks for matches, and extracts matched parts into variables for use.
Execution Sample
Bash Scripting
text="Name: John, Age: 30"
if [[ $text =~ Name: ([A-Za-z]+), Age: ([0-9]+) ]]; then
  name=${BASH_REMATCH[1]}
  age=${BASH_REMATCH[2]}
fi
 echo "$name is $age years old"
Extracts name and age from a text string using capture groups in a regex.
Execution Table
StepActionRegex Match ResultBASH_REMATCH ContentVariables SetOutput
1Set text variable----
2Apply regex to textMatch found["Name: John, Age: 30", "John", "30"]--
3Extract name from BASH_REMATCH[1]--name=John-
4Extract age from BASH_REMATCH[2]--age=30-
5Print output---John is 30 years old
💡 Regex matched successfully, capture groups extracted, output printed.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
text"Name: John, Age: 30""Name: John, Age: 30""Name: John, Age: 30""Name: John, Age: 30"
BASH_REMATCHempty["Name: John, Age: 30", "John", "30"]["Name: John, Age: 30", "John", "30"]["Name: John, Age: 30", "John", "30"]
nameemptyJohnJohnJohn
ageemptyempty3030
Key Moments - 3 Insights
Why do we use double brackets [[ ]] for regex matching instead of single brackets [ ]?
Double brackets [[ ]] support regex matching with =~ operator in Bash, while single brackets [ ] do not. This is shown in Step 2 where regex matching happens.
What does BASH_REMATCH[0] contain compared to BASH_REMATCH[1] and [2]?
BASH_REMATCH[0] contains the full matched string, while BASH_REMATCH[1], [2], etc. contain the capture groups. See Step 2's BASH_REMATCH content.
What happens if the regex does not match the text?
The if condition fails, no variables are set from BASH_REMATCH, and no output is printed. This is the 'No' branch in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 2. What is the content of BASH_REMATCH?
A["Name: John, Age: 30", "John", "30"]
B["John", "30"]
C["Name: John, Age: 30"]
Dempty
💡 Hint
Check the 'BASH_REMATCH Content' column in Step 2 of the execution table.
At which step is the variable 'age' assigned a value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Variables Set' column in the execution table.
If the input text was 'Name: Alice, Age: 25', how would the variable 'name' change in the variable tracker?
AIt would remain empty
BIt would be set to 'Alice'
CIt would be set to 'John'
DIt would cause an error
💡 Hint
The variable 'name' is set from the first capture group, see variable_tracker and execution_table Step 3.
Concept Snapshot
Capture groups in Bash use [[ string =~ regex ]] to match.
Matched groups are stored in BASH_REMATCH array.
BASH_REMATCH[0] is full match; [1], [2], etc. are capture groups.
Use capture groups to extract parts of strings easily.
Always check if regex matched before using BASH_REMATCH.
Full Transcript
This visual trace shows how Bash uses capture groups in regex matching. First, a text string is set. Then, the regex with capture groups is applied using double brackets and the =~ operator. If the regex matches, Bash fills the BASH_REMATCH array with the full match and each capture group. We extract the desired parts from BASH_REMATCH[1], BASH_REMATCH[2], etc., and assign them to variables. Finally, we use these variables, for example, printing the extracted name and age. If the regex does not match, no variables are set and no output is printed. This step-by-step helps beginners see how capture groups work in Bash scripting.