LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 12-21-2011, 02:53 PM   #1
bucovaina78
Member
 
Registered: Oct 2004
Location: Belgium / Antwerp
Distribution: Debian,Ubuntu,Finnix,LFS
Posts: 237

Rep: Reputation: 31
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.

Last edited by bucovaina78; 12-21-2011 at 02:54 PM.
 
Old 12-21-2011, 02:56 PM   #2
bucovaina78
Member
 
Registered: Oct 2004
Location: Belgium / Antwerp
Distribution: Debian,Ubuntu,Finnix,LFS
Posts: 237

Original Poster
Rep: Reputation: 31
Damn, I accidently clicked solved, can I un-solve this? (sorry)

Ok the unsolving is solved
 
Old 12-21-2011, 03:11 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Quote:
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):
Quote:
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.

Last edited by Dark_Helmet; 12-21-2011 at 03:12 PM.
 
1 members found this post helpful.
Old 12-21-2011, 03:45 PM   #4
bucovaina78
Member
 
Registered: Oct 2004
Location: Belgium / Antwerp
Distribution: Debian,Ubuntu,Finnix,LFS
Posts: 237

Original Poster
Rep: Reputation: 31
Ok thanks, that clarifies a bit! I never tried to cat w/o arguments

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

THX
 
Old 12-21-2011, 03:50 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

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


Reply

Tags
redirection, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need an explanation of here scripts as well as the "cat" command UNGR Linux - Newbie 11 08-13-2011 10:08 AM
[SOLVED] "tail -n 1 filename" error while "head -n 1 filename" is ok? type8code0 Linux - Newbie 3 03-21-2011 06:10 AM
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 03:30 PM
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 6 07-09-2008 04:58 PM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 03:37 PM


All times are GMT -5. The time now is 03:24 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration