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
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
| Flag | What it does |
|---|---|
| -w | Count words (runs of non-whitespace) |
| -l | Count lines |
| -c / -m | Count 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.