0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Count Words in a File

Use the command wc -w filename in a Bash script to count the number of words in a file.
📋

Examples

Inputfile.txt content: "Hello world"
Output2 file.txt
Inputfile.txt content: "This is a simple test file."
Output6 file.txt
Inputempty.txt content: ""
Output0 empty.txt
🧠

How to Think About It

To count words in a file, think of reading the file and counting each group of characters separated by spaces or new lines. The wc command in Bash can do this easily with the -w option, which counts words directly without manual parsing.
📐

Algorithm

1
Get the filename as input.
2
Use the <code>wc -w</code> command on the file to count words.
3
Print the word count along with the filename.
💻

Code

bash
#!/bin/bash

# Check if filename is provided
if [ -z "$1" ]; then
  echo "Usage: $0 filename"
  exit 1
fi

# Count words in the file
word_count=$(wc -w < "$1")

# Print the result
echo "$word_count $1"
Output
6 file.txt
🔍

Dry Run

Let's trace counting words in a file named file.txt containing 'This is a simple test file.'

1

Check filename argument

Filename is 'file.txt', so proceed.

2

Count words using wc

Run wc -w < file.txt which returns '6'.

3

Print output

Print '6 file.txt' to the screen.

StepActionValue
1Filename checkfile.txt
2Word count command6
3Output6 file.txt
💡

Why This Works

Step 1: Using wc command

The wc command counts words, lines, and bytes. The -w option tells it to count only words.

Step 2: Redirecting file input

Using < filename feeds the file content to wc without printing the filename twice.

Step 3: Storing and printing result

The word count is stored in a variable and printed with the filename for clarity.

🔄

Alternative Approaches

Using cat and wc
bash
#!/bin/bash
if [ -z "$1" ]; then
  echo "Usage: $0 filename"
  exit 1
fi
word_count=$(cat "$1" | wc -w)
echo "$word_count $1"
This uses <code>cat</code> to send file content to <code>wc</code>, but is less efficient than input redirection.
Using awk
bash
#!/bin/bash
if [ -z "$1" ]; then
  echo "Usage: $0 filename"
  exit 1
fi
word_count=$(awk '{ total += NF } END { print total }' "$1")
echo "$word_count $1"
This counts words by summing fields per line with <code>awk</code>, useful if you want more control.

Complexity: O(n) time, O(1) space

Time Complexity

The script reads the entire file once to count words, so time grows linearly with file size.

Space Complexity

The script uses constant extra space, only storing the count and filename.

Which Approach is Fastest?

Using wc -w with input redirection is fastest and simplest; alternatives like awk offer flexibility but may be slower.

ApproachTimeSpaceBest For
wc -w with input redirectionO(n)O(1)Fastest and simplest word count
cat + wc -wO(n)O(1)Less efficient, but common in scripts
awk counting fieldsO(n)O(1)Flexible word counting with customization
💡
Use wc -w filename for a quick and reliable word count in Bash.
⚠️
Beginners often forget to check if the filename argument is provided, causing errors.