Penguin
Blame: HereDocuments
EditPageHistoryDiffInfoLikePages
Annotated edit history of HereDocuments version 7, including all changes. View license author blame.
Rev Author # Line
1 CraigBox 1 You may have seen scripts containing something like this:
5 BenStaz 2 <verbatim>
7 CraigBox 3 cat << EOF
4 What you are about to do is a really really really bad thing.
5 Only do this if you are absolutely sure you want to do it.
1 CraigBox 6
7 CraigBox 7 Are you absolutely sure that you want to do this? [[y/N]
8 EOF
5 BenStaz 9 </verbatim>
1 CraigBox 10 (Well, you won't have seen anything like that, because the UnixWay is to shoot first and never ask questions!)
11
5 BenStaz 12 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 <<.
1 CraigBox 13
5 BenStaz 14 Here documents are handy for issuing multi-line commands to *interactive* programs such as sed, ftp, cat etc.
1 CraigBox 15
5 BenStaz 16 The format is:
17 <verbatim>
3 CraigBox 18 <<[[-] word
5 BenStaz 19 Line 1
20 Line 2
21 Line 3
22 delimiter
23 </verbatim>
1 CraigBox 24
5 BenStaz 25 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.
1 CraigBox 26
5 BenStaz 27 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.
1 CraigBox 28
7 CraigBox 29 <pre>
1 CraigBox 30 foo <<< word
7 CraigBox 31 </pre>
1 CraigBox 32
33 Why would you use such a thing? For documentation, compare:
34
7 CraigBox 35 <pre>
36 cat << EOF
37 foo bar
38 foo bar
39 bar foo
40 bar baz foo
41 foo bar foo
42 EOF
43
44 echo foo bar
45 echo foo bar
46 echo bar foo
47 echo bar baz foo
48 echo
49 echo foo bar foo
50 </pre>
1 CraigBox 51
3 CraigBox 52 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?)
1 CraigBox 53
54 Another very important use is automating commands such as FTP that don't otherwise provide a method for scripting;
4 BenStaz 55 <verbatim>
1 CraigBox 56 ftp -n <<EOF
57 open ftp.example.org
58 user anonymous ${USER}@
59 cd /path/to/file
60 binary
6 BenStaz 61 passiveDocum
1 CraigBox 62 get file.gz2
63 EOF
4 BenStaz 64 </verbatim>
7 CraigBox 65
1 CraigBox 66 See bash(1).
6 BenStaz 67
68 So Remember, if you ever need to automate an interactive bash program keep Here Documents in mind.
69 BUT Don't forget that sometimes it's easier to have an already made file containing all the commands!
70
71 eg:
72
7 CraigBox 73 * ftp -n < ftp_instructions

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach()