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

wc

In this page:

  1. wc

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)"

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.