Penguin
Diff: HereDocuments
EditPageHistoryDiffInfoLikePages

Differences between current version and predecessor to the previous major change of HereDocuments.

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 7 Last edited on Thursday, May 3, 2007 2:45:52 pm by CraigBox
Older page: version 6 Last edited on Sunday, December 10, 2006 12:07:29 am by BenStaz Revert
@@ -1,12 +1,12 @@
 You may have seen scripts containing something like this: 
 <verbatim> 
- cat << EOF  
- What you are about to do is a really really really bad thing.  
- Only do this if you are absolutely sure you want to do it. 
+cat << EOF  
+What you are about to do is a really really really bad thing.  
+Only do this if you are absolutely sure you want to do it. 
  
- Are you absolutely sure that you want to do this? [[y/N]  
- EOF 
+Are you absolutely sure that you want to do this? [[y/N]  
+EOF 
 </verbatim> 
 (Well, you won't have seen anything like that, because the UnixWay is to shoot first and never ask questions!) 
  
 This is what is called a __Here Document__. The << operator instructs the shell to read your typed input and use all those lines as interactive input to the command that occurred before the <<. 
@@ -25,13 +25,30 @@
 Almost always it is the case that delimiter=word but it is possible that ''word'' and ''delimeter'' may differ (especially if any of the characters in ''word'' are quoted), but that's incredibly rare. If you need to know the intracacies, see the bash page. If you put a - after the << then all leading tab characters are stripped from the input lines; this means you can lay out commands as you would normally and they will all be entered without the tabs. 
  
 A variant on the here document is the [Here String|HereStrings] which uses three <'s to expand a word and supply it to the command on stdin. 
  
+<pre>  
  foo <<< word 
+</pre>  
  
 Why would you use such a thing? For documentation, compare: 
  
-| cat << EOF %%% foo bar %%% foo bar %%% bar foo %%% bar baz foo %%% %%% foo bar foo %%% EOF | echo foo bar %%% echo foo bar %%% echo bar foo %%% echo bar baz foo %%% echo %%% echo foo bar foo 
+<pre>  
+ cat << EOF  
+ foo bar  
+ foo bar  
+ bar foo  
+ bar baz foo  
+ foo bar foo  
+ EOF  
+  
+ echo foo bar  
+ echo foo bar  
+ echo bar foo  
+ echo bar baz foo  
+ echo  
+ echo foo bar foo  
+</pre>  
  
 Then compare that in a twenty line block. Makes writing the documentation easier and checking it painless (did you mean to mention 'echo' at the beginning of a line and your mind skipped over it because all the lines began with echo?) 
  
 Another very important use is automating commands such as FTP that don't otherwise provide a method for scripting; 
@@ -44,12 +61,13 @@
  passiveDocum 
  get file.gz2 
  EOF 
 </verbatim> 
+  
 See bash(1). 
  
 So Remember, if you ever need to automate an interactive bash program keep Here Documents in mind. 
 BUT Don't forget that sometimes it's easier to have an already made file containing all the commands! 
  
 eg: 
  
-*ftp -n < ftp_instructions 
+* ftp -n < ftp_instructions