3

Chapter 43. Redirecting Input and Output of Unix Power Tools, Third Edition has to say this about cat command:

Standard input (stdin) normally comes from your keyboard. Many programs ignore stdin; you name files directly on their command line — for instance, the command cat file1 file2 never reads its standard input; it reads the files directly. But without filenames on the command line, Unix commands that need input will usually read stdin. Standard input normally comes from your keyboard, but the shell can redirect stdin from a file.

(emphasis mine)

Ok but happens when we just type cat > filename in the command line? Isn't cat reading from the stdin and stores that that into file "filename"? Is the above excerpt from the book just saying that only the particular form of using cat with a FILE argument never reads from the stdin?

1
  • it says "the command cat file1 file2"
    – user20574
    Commented Dec 30, 2020 at 22:00

1 Answer 1

4

Isn't cat reading from the stdin and stores that that into file "filename"?

Yes, when cat does not have any filename arguments (or if one of the files is the minus character -), it reads from stdin.

Perhaps use of the word "never" by the book is a bit misleading, because:

Is the above excerpt from the book just saying that only the particular form of using cat with a FILE argument never reads from the stdin?

Yes, in that particular instance, cat will not touch stdin.

6
  • What is the speciality of the - character that leads cat to read from stdin?
    – Geek
    Commented Jul 24, 2014 at 19:31
  • 3
    It's just a common feature of programs to use - to mean stdin, but the program does actually have to support it. If a program just tries to open a file named -, it'll open that file instead of stdin Commented Jul 24, 2014 at 19:32
  • And under some systems such as Linux, if the program doesn't support -, one can use /dev/stdin instead.
    – vinc17
    Commented Jul 24, 2014 at 23:12
  • The - filename argument allows you to say cat file1 - file2 > great_scott to get cat to read first from file1, then from the terminal, and then from file2. Commented Jul 25, 2014 at 2:47
  • 4
    It is not misleading to say never in that sentence. One just has to realize that the sentence explicitly speaks about the command cat file1 file2. It doesn't say anything about what cat does when invoked with different arguments.
    – kasperd
    Commented Aug 25, 2015 at 7:41

You must log in to answer this question.

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