← Back to Bash Course | Chapter 11: Process Management | Lesson 11 of 12

wc

wc is like a counter that tells you how many lines, words or letters are in some text. It is handy at the end of a chain to see how much made it through.

In this page:

  1. wc
Syntax
bash
wc [-l | -w | -c] file_name

wc

wc counts lines (-l), words (-w), and characters/bytes (-c) in its input. It's often placed at the end of a pipeline to summarize how much data made it through previous filtering steps.

Running it with no flags prints all three counts together.

Note: command | wc -l is the standard idiom for counting how many lines a command produced.

Example: wc

bash
#!/bin/bash
printf "one\ntwo\nthree\n" > words.txt

echo "Lines: $(wc -l < words.txt)"
echo "Words: $(wc -w < words.txt)"
echo "Characters: $(wc -c < words.txt)"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using wc -l and expecting the last line to count without a trailing newline.
  2. Reading wc -l file output as only a number, when it includes the file name.
  3. Confusing -c (bytes) and -m (characters).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.