Linuxlab

Regular expressions in Linux: basics for grep and sed

Updated July 3, 2026

A regular expression is a pattern that describes a set of text strings.

Common cases

.           # any single character
^start      # anchor to the start of the line
end$        # anchor to the end of the line
[0-9]+      # one or more digits (grep -E)
error|fail  # match one or the other (grep -E)

How it works: basic versus extended

Regular expressions have two dialects in the GNU tools: basic (BRE) and extended (ERE). In basic, the characters +, ?, | and () are literal, and to give them special meaning you escape them: \+. The grep -E flag (and sed -E) turns on the extended dialect, where they work directly: error|fail, [0-9]+. Mind greediness: .* grabs as much as it can. And do not confuse a regular expression with shell globbing: * in the shell and * in a regex mean different things.

Used in

You need this in the lesson Text: grep, sed, awk. The theory is the chapter Bash in depth.

See also

The grep command, The sed command, The awk command.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.