2

Regarding wc (word count) command... I am trying to understand the following (from man wc which I have quoted below plus added a longer quote at end of my question)

With no FILE, or when FILE is -, read standard input.

How exactly does this work? At what point do I type the "standard input"?

Finding explanations online rather confusing. Maybe I am just missing some basic info regarding what exactly stdin is.

From man wc

SYNOPSIS 
     wc [OPTION]... [FILE]... wc [OPTION]... --files0-from=F
         
DESCRIPTION 
     Print newline, word, and byte counts for each FILE, and a total
     line if more than one FILE is specified. A word is a
     non-zero-length sequence of characters delimited by white space.
         
     With no FILE, or when FILE is -, read standard input.

2 Answers 2

3

wc, the program, detects when there's 0 filenames, or just "-", on the command line, e.g.

wc
wc -

and, in those cases, reads from -STDIN (Stream 0 for the process). You never need to "type standard input".

The "parent process", the one that creates a process, is responsible for setting up 3 I/O streams:

0 STDIN - program input
1 STDOUT - normal program output
2 STDERR - error program output

It's up to the program to read from STDIN, if it wants to.

An example of using wc to read from the standard input stream:

$ echo 'hello world' | wc
      1       2      12

Here, wc counts the number of lines, words, and characters arriving on its standard input stream. That stream is connected to the standard output stream of the echo command by the shell via a pipe. The echo command sends the string hello world to its standard output stream, and wc receives it.

1
  • Of course, if there is no redirection from a file, pipe, HereDoc or HereString, the default is that wc - will have the terminal as its stdin. In which case, you type your "file" directly into the keyboard, and terminate that file by typing Ctrl-D on a line by itself. This kind of agnostic inheritance is a fundamental part of Unix-like systems. Nov 14, 2021 at 16:52
0

My issue was that I was unfamiliar with using ctrl+d to "end terminal line input" (after typing input text).

I suppose it's basic knowledge for using the command line - but something I was unable to figure out by reading the man page and googling how to use the wc command extensively.

Step by step for any other confused beginners out there:

Type wc - in terminal and hit ENTER.

Now you can start typing your input.

When you are done, press ctrl+d

2
  • It's not on the man page, or most other things about wc, because it's not specific to or different in any way for wc -- it's the same for nearly ALL programs on Unix; see en.wikipedia.org/wiki/Standard_streams Nov 15, 2021 at 1:08
  • Yes these kinds of realizations come more quickly and easily with some experience. Nov 15, 2021 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .