Course contentsShow
← Linux
Lesson 247 of 1,8192. Deep Dive into Bash ScriptingFree lesson

Pattern Matching Engines

Compare globbing vs regex performance within the shell.

In this lesson, you will learn how the shell’s built-in globbing engine differs from regular expressions in terms of processing performance and internal mechanics.

The Mechanism of Globbing

When you use wildcards like *.log in your shell, you are utilizing the shell’s globbing engine. This is a lightweight, built-in feature designed specifically for filesystem path expansion. Because it is part of the shell’s core logic, it is incredibly fast. It operates by converting your pattern into a simple set of rules that match filenames directly as the shell scans the directory entries. It doesn't need to fork external processes; it uses the shell's internal memory management to filter results immediately.

The Complexity of Regex

Regular Expressions (regex), conversely, are processed by more powerful engines like grep, sed, or awk. These are external binaries (per our lesson on Built-in vs Binary). When you pass a regex to these tools, the shell must perform a fork to create a new process, load the binary into memory, and then invoke the regex engine.

Regex engines are built to handle complex string parsing—they track state, handle back-references, and perform backtracking. While powerful, this adds significant overhead compared to globbing.

Performance Comparison

Imagine you need to find all files ending in .txt.

  • Globbing: ls *.txt The shell internally expands this list before running any command. It is near-instant because it stays within the shell's memory space.
  • Regex: find . | grep '\.txt$' The shell forks a find process, pipes the output, and forks a grep process. The overhead of process creation and pipe communication makes this significantly slower for simple file matching.

Key Takeaway: Use globbing for simple path matching because it is a lightweight, built-in shell operation; reserve regex for complex string manipulation where the overhead of external binaries is justified by the advanced pattern control they provide.