Differences between version 5 and predecessor to the previous major change of HereDocuments.
Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 5 | Last edited on Saturday, December 9, 2006 11:58:16 pm | by BenStaz | Revert |
Older page: | version 3 | Last edited on Monday, November 11, 2002 11:08:31 am | by CraigBox | Revert |
@@ -1,27 +1,31 @@
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.
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 input from the current source (most commonly the script file that is being interpreted)
and use all those lines as standard
input for
the command that occured
before the <<.
+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 <<.
-The format (
as defined by bash(1)) is:
+Here documents are handy for issuing multi-line commands to *interactive* programs such
as sed, ftp, cat etc.
+The format is:
+<verbatim>
<<[[-] word
- here-document
- delimeter
+ Line 1
+ Line 2
+ Line 3
+ delimiter
+</verbatim>
+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.
-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__,
which uses three <'s to expand a word and supply it to the command on stdin.
+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.
foo <<< word
Why would you use such a thing? For documentation, compare:
@@ -30,15 +34,15 @@
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;
-
+<verbatim>
ftp -n <<EOF
open ftp.example.org
user anonymous ${USER}@
cd /path/to/file
binary
passive
get file.gz2
EOF
-
+</verbatim>
See bash(1).