Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
What is the speciality of the - character that leads cat to read from stdin? –  Geek Jul 24 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 –  Drew McGowen Jul 24 at 19:32
    
And under some systems such as Linux, if the program doesn't support -, one can use /dev/stdin instead. –  vinc17 Jul 24 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. –  Scott Jul 25 at 2:47
    
This is actually not correct: Isn't cat reading from the stdin and stores that that into file "filename"? cat does read from stdin but it does not concatenate that to "filename", exactly, at least not in the sense most processes do. cat always writes to stdout - a file descriptor inherited from its parent process. But its parent does all of the opening and assigning and whatever - as is indicated by the shell's > redirection operator. cat just shoots - the parent process has to do all filename aiming. –  mikeserv Jul 25 at 4:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.