Skip to content
Latchkey

wc -w: Count Words

wc -w counts words, defined as maximal runs of non-whitespace characters.

wc -w counts tokens, useful for sanity checks like "did this command return any items?" on a single line.

What it does

wc -w counts words, where a word is a non-empty run of non-whitespace separated by whitespace. It is line-independent, so a single line of space-separated values reports the number of values, which line counting cannot do.

Common usage

Terminal
echo "$LIST" | wc -w               # how many space-separated items
wc -w document.md                   # word count of a doc
ls | wc -w                          # entries on one line (caution: spaces)

Options

FlagWhat it does
-wCount words (runs of non-whitespace)
-lCount lines
-c / -mCount bytes / characters

In CI

wc -w is a quick way to count items in a single-line, space-separated variable when wc -l would report 1. For filenames with spaces, prefer counting null-delimited output instead, since wc -w miscounts those.

Common errors in CI

Empty input gives 0, but a value that is only whitespace also gives 0, which can mask a real difference; check separately if that matters. Filenames containing spaces are counted as multiple words by ls | wc -w; use find -print0 and a null-aware counter instead. Locale affects what counts as whitespace; set LC_ALL=C for predictable splitting.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →