Bash Scripting — Using Regular Expressions in Scripts
What Are Regular Expressions?
Regular expressions (regex) are patterns that describe sets of strings. They’re one of the most powerful tools in your scripting arsenal — letting you search, validate, extract, and transform text with surgical precision.
If you’ve been following along from the Part 2 grep chapter, you’ve already seen basic regex in action. This chapter takes it further — we’ll embed regex deeply into your Bash scripts using grep, sed, awk, and Bash’s native =~ operator.
Why Regex Matters in Scripts
Think about the tasks you automate daily:
- Validating user input (emails, IPs, dates)
- Extracting version numbers from log files
- Replacing config values on the fly
- Filtering lines that match complex patterns
All of these become trivial once you’re comfortable with regex.
Chapter Roadmap
This chapter is organised into focused lessons. Work through them in order — each builds on the last.
| # | Topic | What You’ll Learn |
|---|---|---|
| 1 | grep -E in Scripts | BRE vs ERE, grep -E, grep -P, quiet mode |
| 2 | Bash =~ Operator | Native regex matching, BASH_REMATCH, capture groups |
| 3 | POSIX Character Classes | [[:digit:]], [[:alpha:]], locale-aware patterns |
| 4 | sed Regex in Scripts | sed -E, back-references, multi-line tricks |
| 5 | awk Regex Patterns | Pattern-action blocks, match(), gensub() |
| 6 | Practice Exercises | 5 hands-on challenges from easy to advanced |
Quick Regex Refresher
Before diving in, here’s a cheat sheet of the most common regex metacharacters:
| Symbol | Meaning | Example |
|---|---|---|
. |
Any single character | a.c → abc, aXc |
* |
Zero or more of previous | ab*c → ac, abc, abbc |
+ |
One or more of previous (ERE) | ab+c → abc, abbc |
? |
Zero or one of previous (ERE) | colou?r → color, colour |
^ |
Start of line | ^Hello |
$ |
End of line | world$ |
[] |
Character class | [aeiou] |
[^] |
Negated class | [^0-9] |
\| |
Alternation (ERE) | cat\|dog |
() |
Grouping (ERE) | (ab)+ |
{} |
Repetition (ERE) | a{2,4} → aa, aaa, aaaa |
BRE vs ERE — The Two Flavours
You’ll see two terms throughout this chapter:
- BRE (Basic Regular Expressions) — used by
grepandsedby default. You must escape+,?,|,(,),{,}with a backslash. - ERE (Extended Regular Expressions) — used by
grep -E,sed -E, andawk. Metacharacters work without escaping.
# BRE — must escape the +
grep 'ab\+c' file.txt
# ERE — cleaner syntax
grep -E 'ab+c' file.txt
Tip: Always prefer ERE (-E flag) in scripts — it’s more readable and less error-prone.
Prerequisites
Make sure you’re comfortable with:
- Basic Bash scripting (variables, conditionals, loops)
- The
grepcommand basics from Part 2 - Piping and redirection
Let’s Begin!
Start with grep -E in Scripts →
Chapter 26 of the Linux Bash Scripting series on timesofcloud.com.