[grep] Match whole words with grep -w

Created on November 30, 2026

grep -w selects lines where a match has word boundaries on both sides. This example searches for the word cat.

printf '%s\n' 'cat' 'concatenate' 'cat dog' 'cat_1' 'cat-1' | grep -w 'cat'
cat
cat dog
cat-1

Letters, digits, and underscores count as word characters. For the sample input, a space or hyphen separates cat from adjacent text. The character rules can depend on the locale. Use grep -x when the whole line must match.

Reference

Shell command grep