-   Linux - General (
-   -   cat > filename << "EOF" syntax explanation (

bucovaina78 12-21-2011 03:53 PM

cat > filename << "EOF" syntax explanation
Ok I know what this does and how to apply it but I can't explain how it works exactly.

How and why does cat generate a file all of a sudden?

Why doensn't bash interpret my ENTER keystroke as "yes this is the full command?" Why does it wait until I type EOF?

I'm guessing it's all in the redirection of in/output.

bucovaina78 12-21-2011 03:56 PM

Damn, I accidently clicked solved, can I un-solve this? (sorry)

Ok the unsolving is solved :)

Dark_Helmet 12-21-2011 04:11 PM


Originally Posted by bucovaina78
I'm guessing it's all in the redirection of in/output.

Well... yes! :)

The sole purpose of cat is (according to the man page):

cat - concatenate files and print on the standard output
So, as an experiment, try running "cat" alone on the command line. You'll see that it echoes everything you type as soon as you press enter. You repeat the process until you press Control-D (which sends an "end-of-file" character).

Just like any other command, if you use output redirection (the '>'), your shell will take the standard output of the command and write it to a file instead of displaying it on a screen. So, it's the shell (e.g. bash, ksh, tcsh, etc.) that creates the file--not the cat command.

Similarly, the input redirection basically says "pretend that the text after '<<' indicates the end of the input".

Putting them together, the cat command is behaving exactly as though it were given no arguments. Your shell is the one moving data around. As you type, the shell is looking for "EOF" to signal the end of the input. When that text is seen, it sends cat an end-of-file signal so that cat stops processing things. Also, cat is echoing everything you type as before, except that as it's echo'd, your shell is grabbing that text and writing it to a file--so you never see it displayed.

bucovaina78 12-21-2011 04:45 PM

Ok thanks, that clarifies a bit! I never tried to cat w/o arguments

I'm going to mark this thread as SOLVED (again :) )


colucix 12-21-2011 04:50 PM

It's worth reading about here documents (this is the name given to this peculiar construct) in the Advanced Bash Scripting Guide. :twocents:

All times are GMT -5. The time now is 04:05 AM.