Examples of Regular Expressions in Shell Scripts: 3 Practical Examples You’ll Actually Use

If you write shell scripts for anything beyond toy projects, you eventually run into regular expressions. And when that happens, you don’t want vague theory—you want concrete examples of regular expressions in shell scripts: 3 practical examples that map directly to real-world tasks. That’s what this guide focuses on. We’ll walk through three of the best examples of regular expressions in shell scripts, then expand each one with variations you can copy and adapt. You’ll see how to validate input, filter logs, and automate text processing with `bash`, `grep`, and `awk`—all using patterns that show up in real production scripts. Along the way, we’ll talk about why these patterns matter in 2024–2025, when log volumes are exploding, CI pipelines are everywhere, and data cleanup is half the job. If you’ve ever stared at a regex like `/^[A-Za-z0-9._%+-]+@/` and thought, “I know this works, but I don’t know why,” this article is for you.
Written by
Jamie
Published

Let’s start with the most common example of using regex in shell scripts: validating user input. In 2024–2025, shell scripts still sit behind CI jobs, deployment hooks, and quick admin tools. Bad input in those places can mean broken pipelines or misconfigured servers.

Here’s a simple pattern: validate an email address in a Bash script using [[ ... =~ ... ]].

#!/usr/bin/env bash

read -rp "Enter your email: " email

## Basic email regex (good enough for most CLI tools)
email_regex='^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'

if [[ \(email =~ \)email_regex ]]; then
  echo "Valid email: $email"
else
  echo "Invalid email format" >&2
  exit 1
fi

This is one of the best examples of regular expressions in shell scripts: short, readable, and immediately useful. The anchors ^ and $ ensure the whole string must match. The character classes keep the pattern from accepting obvious garbage.

Variations: more real examples of validation patterns

When people ask for examples of regular expressions in shell scripts: 3 practical examples, input validation usually takes up at least one slot. But it’s rarely just email. Real scripts often validate:

  • Usernames
  • IPv4 addresses
  • Semantic versions
  • Dates

Here are a few real examples you can drop into your scripts.

Username: letters, digits, underscores, 3–16 chars

username_regex='^[a-zA-Z0-9_]{3,16}$'

if [[ \(username =~ \)username_regex ]]; then
  echo "Username OK"
else
  echo "Username must be 3–16 characters (letters, digits, underscore)" >&2
fi

This pattern shows how you can enforce both allowed characters and length with a single regex.

IPv4 address (simple, not perfect)

ip_regex='^([0-9]{1,3}\.){3}[0-9]{1,3}$'

if [[ \(ip =~ \)ip_regex ]]; then
  echo "Looks like an IPv4 address"
else
  echo "Not an IPv4 address" >&2
fi

This is a real example of a “good enough” regex: it doesn’t block values like 999.999.999.999, but it filters out non-IP-like junk quickly. If you need stricter validation, you can combine this with a numeric check:

IFS='.' read -r o1 o2 o3 o4 <<< "$ip"
for octet in "\(o1" "\)o2" "\(o3" "\)o4"; do
  if (( octet < 0 || octet > 255 )); then
    echo "Invalid IPv4 range" >&2
    exit 1
  fi
done

This pairing—regex for structure, numeric checks for constraints—is one of the best examples of how shell scripts and regex complement each other.

Semantic version (e.g., 1.2.3 or 2.0.0-beta)

semver_regex='^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$'

if [[ \(version =~ \)semver_regex ]]; then
  echo "Version string accepted"
else
  echo "Invalid version format" >&2
fi

In CI/CD systems (GitHub Actions, GitLab CI, Jenkins), this kind of regex-based check still shows up constantly in 2024–2025. It’s simple, auditable, and doesn’t require extra dependencies.


2. Log filtering and monitoring: examples include grep and awk

If you’re working on Linux servers, examples of regular expressions in shell scripts: 3 practical examples almost always include some kind of log filtering. Shell is still the fastest way to triage issues from the command line.

Here’s a baseline pattern: filter HTTP access logs for 5xx errors from a specific IP.

#!/usr/bin/env bash

log_file="/var/log/nginx/access.log"
client_ip="203.0.113.42"

## Show lines where status code starts with 5 and matches the IP
awk -v ip="\(client_ip" '\)1 == ip && \(9 ~ /^5[0-9][0-9]\)/ { print }' "$log_file"

This script uses awk with a regex on the status code field (\(9). The pattern ^5[0-9][0-9]\) matches any 500-level HTTP status.

Real examples of log regex patterns you actually need

Let’s extend this with a few more real examples that show up in production troubleshooting.

Find repeated failed SSH logins

#!/usr/bin/env bash

log_file="/var/log/auth.log"

## Extract failed SSH attempts and summarize by IP
grep -E 'Failed password for (invalid user )?.* from [0-9.]+' "$log_file" \
  | sed -E 's/.* from ([0-9.]+) port.*/\1/' \
  | sort \
  | uniq -c \
  | sort -nr | head

The regex inside grep -E:

Failed password for (invalid user )?.* from [0-9.]+

This is a good example of using groups and optional parts: (invalid user )? matches both “invalid user” and regular user failures. [0-9.]+ captures the IPv4 address.

Extract slow queries from a database log

Imagine a log line like:

2025-01-02 12:34:56 [slow] query_time=3.42s SELECT * FROM users;

You might want to flag queries slower than 1 second.

#!/usr/bin/env bash

log_file="/var/log/db/queries.log"

awk '/\[slow\]/ && \(0 ~ /query_time=[0-9.]+s/ { print }' "\)log_file"

The regex /query_time=[0-9.]+s/ finds lines with a numeric duration. If you want to be stricter, you can capture only times starting with 1 or more seconds:

awk '\(0 ~ /query_time=([1-9][0-9]*|[1-9])[0-9.]*s/ { print }' "\)log_file"

This is an advanced example of using alternation and grouping in a shell-friendly way.

Filter logs by ISO 8601 timestamp pattern

With modern services and cloud-native apps, ISO 8601 timestamps are everywhere. Here’s a regex that matches a basic timestamp with T and Z:

iso_regex='^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z'

grep -E "$iso_regex" app.log | head

You can use this pattern inside scripts to split logs by date, count errors per hour, or feed data into monitoring tools.


3. Text processing and data cleanup: the quiet workhorse

The third slot in our examples of regular expressions in shell scripts: 3 practical examples goes to text processing. It’s not glamorous, but in data pipelines and ETL-style jobs, shell scripts still glue everything together.

Think of tasks like:

  • Normalizing phone numbers
  • Stripping comments from config files
  • Extracting fields from CSV or TSV

Here’s a real example of cleaning and normalizing phone numbers from a text file.

#!/usr/bin/env bash

input="phones.txt"

## Normalize US-style phone numbers to digits only, 10 or 11 characters
while IFS= read -r line; do
#  # Remove everything except digits
  digits=\((echo "\)line" | tr -cd '0-9')

#  # Match 10-digit numbers or 11-digit numbers starting with 1
  if [[ \(digits =~ ^1?[0-9]{10}\) ]]; then
    echo "$digits"
  fi

done < "$input"

The regex ^1?[0-9]{10}$ is one of the best examples of a concise pattern: an optional leading 1 (country code), followed by exactly 10 digits.

Stripping comments and blank lines from config files

Config cleanup is another place where examples include regular expressions inside shell. Say you want to preprocess a .conf file before feeding it into another tool.

#!/usr/bin/env bash

config="app.conf"

## Remove comments and empty lines
sed -E '/^\s*#/d; /^\s*\(/d' "\)config" > cleaned.conf

The regex '^\s*#' matches any line that starts with optional whitespace followed by #. The pattern '^\s*$' matches blank or whitespace-only lines. Combining them in one sed command gives you a compact, readable script.

Extracting fields from semi-structured data

Modern logs and exports often look like this:

user=jane status=active plan=premium
user=john status=pending plan=free

You can grab the user and plan fields with grep and sed using regex groups.

#!/usr/bin/env bash

log="users.log"

while IFS= read -r line; do
  if [[ $line =~ user=([^[:space:]]+) ]]; then
    user="${BASH_REMATCH[1]}"
  fi
  if [[ $line =~ plan=([^[:space:]]+) ]]; then
    plan="${BASH_REMATCH[1]}"
  fi
  echo "User: \(user, Plan: \)plan"

done < "$log"

The pattern user=([^[:space:]]+) is a classic example of using a capture group to extract a field. [^[:space:]]+ means “one or more non-whitespace characters.”


Why these examples of regular expressions in shell scripts still matter in 2024–2025

You might wonder why, in a world of Python, Go, and Kubernetes operators, we’re still talking about examples of regular expressions in shell scripts: 3 practical examples.

Two reasons stand out:

  • Shell scripts are still the first layer of automation in many organizations: provisioning, deployment hooks, log scraping, and quick health checks.
  • Regular expressions remain the fastest way to express string constraints and search patterns in these scripts without pulling in a full programming language runtime.

Industry surveys and job postings in 2024 show that Linux command-line skills and shell scripting are still requested across DevOps, SRE, and data engineering roles. Even when teams standardize on Python or Go, they keep small shell scripts around for glue logic and quick fixes.

If you understand the best examples of regular expressions in shell scripts—input validation, log filtering, and text cleanup—you can solve a huge chunk of day-to-day automation problems without reaching for heavier tools.

For developers who want to deepen their understanding of patterns and text processing more broadly (beyond shell), it’s worth looking at classic references like the GNU grep manual and the GNU sed manual. While not health-focused, they mirror the kind of authoritative, well-maintained documentation you see on sites like NIST.gov for standards and best practices in computing.


FAQ: quick answers about regex in shell scripts

What are the most common examples of regex in shell scripts?

The most common examples of regex in shell scripts are:

  • Validating input (emails, usernames, IP addresses, versions)
  • Filtering or summarizing logs (HTTP status codes, SSH failures, slow queries)
  • Cleaning and transforming text (removing comments, normalizing phone numbers, extracting fields)

The three best examples we walked through—validation, log filtering, and text processing—cover most real-world use cases.

Can I use the same regular expressions in Bash, grep, and awk?

Mostly, but not always. Bash’s [[ ... =~ ... ]] uses its own flavor of regex. grep -E uses extended POSIX regex, and awk also uses a POSIX-like flavor. Simple patterns like ^[0-9]+$ or ^user= work everywhere. Advanced constructs (like backreferences or non-greedy quantifiers) may behave differently or not exist in some tools.

A practical rule: when you build an example of a pattern for a script, test it explicitly with the tool you plan to use (grep, awk, sed, or Bash itself).

Is it better to use regex or external tools like Python for validation?

For small scripts, regex inside shell is usually faster to write, easier to deploy, and completely fine. If your validation rules get complicated—think strict email RFC compliance or heavy date math—then pulling in Python or another language can be worth it.

In many organizations, the best examples of shell scripting in production combine both: quick regex checks in shell, and heavier validation or transformation in a secondary script written in Python, Go, or another language.

How can I safely test new regular expressions used in shell scripts?

Use small, focused test files and echo commands. For example:

echo "test@example.com" | grep -E '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'

You can also use online regex testers that support POSIX or PCRE syntax, then adapt the patterns for shell. Just be aware that some testers default to PCRE features that grep or Bash won’t understand.

Where can I learn more beyond these 3 practical examples?

Once you’re comfortable with these examples of regular expressions in shell scripts, the next step is learning:

  • POSIX regex syntax in more detail (character classes, anchors, grouping)
  • Tool-specific quirks for grep, sed, awk, and Bash

The GNU manuals are good long-term references:

They’re not light reading, but they’re reliable when you want to understand exactly how a pattern behaves.


If you keep these three best examples of regular expressions in shell scripts in your toolkit—and adapt the variations shown here—you’ll be able to handle most of the input validation, log filtering, and text cleanup that lands on your desk, without overcomplicating your stack.

Explore More Shell Scripting Snippets

Discover more examples and insights in this category.

View All Shell Scripting Snippets