What Is Wc in Shell Scripting?


Larry Thompson

Shell scripting is a powerful tool for automating tasks in a Unix-like operating system. It allows you to write scripts that can execute a series of commands, perform calculations, manipulate files, and much more.

One concept that is frequently used in shell scripting is the ‘wc’ command. Let’s explore what ‘wc’ is and how it can be utilized in shell scripts.

What Is ‘wc’?

‘wc’ stands for word count, and it is a command-line utility that provides information about the number of lines, words, and characters in a file or input stream. It is commonly used to analyze text files or the output of other commands.


The basic syntax of the ‘wc’ command is:

$ wc [options] [file..]

The options allow you to modify the behavior of the command, while the ‘file.’ argument specifies one or more files to analyze. If no file is specified, ‘wc’ reads from standard input.

Available Options:

  • -l: Display the number of lines in the input
  • -w: Display the number of words in the input
  • -c: Display the number of characters in the input

By default, ‘wc’ displays all three counts – lines, words, and characters.

Using ‘wc’ in Shell Scripts:

‘wc’ can be incredibly useful when writing shell scripts that involve text processing. For example, let’s say you have a script that reads from a log file and you want to count how many times a specific keyword appears:



count=$(grep -c "$keyword" "$log_file")

echo "The keyword '$keyword' appears $count times."

In this script, we use the ‘grep’ command to search for the keyword in the log file and the ‘-c’ option to count the number of matches. The result is stored in the ‘count’ variable, which is then displayed using ‘echo’.

Example Usage:

Let’s assume we have a file named ‘example.txt’ with the following content:

Hello, world! This is an example text file. 

It contains multiple lines and words. Have a great day!

If we run the following command:

$ wc example.txt

We will get the following output:

4 12 73 example.txt

The numbers represent the number of lines, words, and characters in the file respectively. In this case, there are 4 lines, 12 words, and 73 characters.


The ‘wc’ command is a versatile tool for analyzing text files or input streams in shell scripts. By using its various options, you can obtain valuable information such as line counts, word counts, and character counts. Incorporating ‘wc’ into your shell scripts can greatly enhance their functionality and provide valuable insights into your data.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy