Hone

Lessons · Terminal · the shell's * and grep's *

Two different stars

The shell's * expands to matching FILE NAMES before the command runs. grep's * means none-or-more of the thing before it.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

They meet on the same command line and mean unrelated things.

How to think about it

Quote the pattern. Then grep gets exactly what you typed.

Worked example

grep 'ab*' pat.txt
An a followed by any number of b's: ab and abb.
grep -c 'ab*' pat.txt
The same two, counted rather than listed.
grep 'b' pat.txt
All three lines contain a b somewhere.

Your turn

Match an a followed by any number of b's.

grep 'a' orders.csv

The trap

Unquoted, a pattern containing * or ? or [ ] may be replaced by file names before grep ever sees it, and the error will be about the wrong thing.

Practise the shell's * and grep's * on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.