0
0
Bash Scriptingscripting~10 mins

Regex in [[ ]] with =~ in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Regex in [[ ]] with =~
Start
Evaluate string
Evaluate regex pattern
Use [[ string =~ regex
If match: return true
If no match: return false
Use result in if/else or variable
End
The shell evaluates the string and regex pattern inside [[ ]]. It tests if the string matches the regex using =~, then returns true or false for conditional use.
Execution Sample
Bash Scripting
text="hello123"
if [[ $text =~ ^hello[0-9]+$ ]]; then
  echo "Match"
else
  echo "No match"
fi
This script checks if the variable 'text' matches the regex pattern for 'hello' followed by one or more digits.
Execution Table
StepActionString ValueRegex PatternMatch ResultOutput
1Assign text variablehello123^hello[0-9]+$
2Evaluate [[ $text =~ regex ]]hello123^hello[0-9]+$true
3If condition true, execute then branchhello123^hello[0-9]+$trueMatch
4End if statementhello123^hello[0-9]+$trueMatch
💡 Regex matches string, condition is true, so 'Match' is printed and script ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
texthello123hello123hello123
match_resulttruetrue
outputMatch
Key Moments - 3 Insights
Why do we use double square brackets [[ ]] instead of single [ ] for regex matching?
Double brackets [[ ]] support the =~ operator for regex matching, while single brackets [ ] do not. See execution_table step 2 where [[ ]] evaluates the regex.
What happens if the regex does not match the string?
The match_result becomes false, so the else branch runs. This is shown by the exit_note explaining the condition outcome.
Can we use quotes around the regex pattern inside [[ ]]?
No, quoting the regex disables regex matching and treats it as a string. The pattern must be unquoted as shown in the code sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the match result at step 2?
Aerror
Bfalse
Ctrue
Dempty
💡 Hint
Check the 'Match Result' column in execution_table row for step 2.
At which step does the script print 'Match'?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in execution_table to find when 'Match' is printed.
If the variable text was 'helloabc', what would be the match result?
Afalse
Btrue
Cerror
Ddepends on shell version
💡 Hint
Refer to variable_tracker and regex pattern: digits expected after 'hello', letters cause no match.
Concept Snapshot
Use [[ string =~ regex ]] to test if string matches regex.
Double brackets [[ ]] enable regex matching with =~ operator.
Regex pattern must be unquoted inside [[ ]].
Returns true if match, false if no match.
Use in if statements to branch logic based on pattern match.
Full Transcript
This example shows how to use regex matching in bash with double square brackets and the =~ operator. We assign a string variable 'text' with value 'hello123'. Then we test if 'text' matches the regex '^hello[0-9]+$' which means 'hello' followed by one or more digits. The [[ $text =~ regex ]] returns true because 'hello123' fits the pattern. The if condition runs the then branch and prints 'Match'. Variables 'text' and 'match_result' keep their values through the steps. Key points: use [[ ]] not [ ], do not quote regex, and the condition result controls the output. This helps check patterns in strings easily in bash scripts.