Skip to content
Latchkey

wc Command Reference for CI Scripts

wc counts lines, words, and bytes of its input.

wc is the go-to for counting matches or sizing output in a pipeline. The subtle CI issue is the leading whitespace it prints, which can break a numeric comparison.

Common flags/usage

  • -l: count lines (newlines)
  • -w: count words
  • -c: count bytes
  • -m: count characters (multibyte aware)
  • -L: length of the longest line

Example

shell
COUNT=$(git diff --name-only | wc -l | tr -d ' ')
if [ "${COUNT}" -gt 100 ]; then echo "Large PR" ; fi
grep -c TODO src/*.ts | awk -F: '{s+=$2} END{print s}'

In CI

On BSD/macOS wc pads its output with leading spaces, so COUNT=$(... | wc -l) may hold " 42" and break [ "$COUNT" -eq 42 ]; strip it with tr -d ' ' or use grep -c. Note wc -c counts bytes while wc -m counts characters, which differ for UTF-8 text.

Key takeaways

  • wc -l counts lines; pipe through tr -d " " to strip padding for comparisons.
  • grep -c often replaces grep | wc -l for counting matches.
  • wc -c counts bytes, wc -m counts characters; they differ for multibyte text.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →