Differences between version 3 and previous revision of HereDocuments.
Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 3 | Last edited on Monday, November 11, 2002 11:08:31 am | by CraigBox | Revert |
Older page: | version 2 | Last edited on Monday, November 11, 2002 11:05:36 am | by CraigBox | Revert |
@@ -3,18 +3,18 @@
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]
+ Are you absolutely sure that you want to do this? [
[y/N]
EOF
(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 <<.
The format (as defined by bash(1)) is:
- <<[-] word
+ <<[
[-] word
here-document
delimeter
@@ -23,14 +23,13 @@
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.
foo <<< word
-%%%
Why would you use such a thing? For documentation, compare:
-| cat << EOF %%% foo bar %%% foo bar %%% foo bar foo %%% EOF | cat
foo bar %%% cat
foo bar %%% cat
foo bar foo
+|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
-Then compare that in a twenty line block. Makes writing the documentation easier and checking it painless (did you mean to mention 'cat
' at the beginning of a line and your mind skipped over it because all the lines began with cat
?)
+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;
ftp -n <<EOF