<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.christophchamp.com/index.php?action=history&amp;feed=atom&amp;title=Here_document</id>
		<title>Here document - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.christophchamp.com/index.php?action=history&amp;feed=atom&amp;title=Here_document"/>
		<link rel="alternate" type="text/html" href="http://wiki.christophchamp.com/index.php?title=Here_document&amp;action=history"/>
		<updated>2026-04-30T16:36:34Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>http://wiki.christophchamp.com/index.php?title=Here_document&amp;diff=7839&amp;oldid=prev</id>
		<title>Christoph: Created page with &quot;In computing, a '''here document''' ('''here-document''', '''here-text''', '''heredoc''', '''hereis''', '''here-string''', or '''here-script''') is a file literal or input str...&quot;</title>
		<link rel="alternate" type="text/html" href="http://wiki.christophchamp.com/index.php?title=Here_document&amp;diff=7839&amp;oldid=prev"/>
				<updated>2020-06-29T00:46:00Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;In computing, a &amp;#039;&amp;#039;&amp;#039;here document&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;here-document&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;here-text&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;heredoc&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;hereis&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;here-string&amp;#039;&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;&amp;#039;here-script&amp;#039;&amp;#039;&amp;#039;) is a file literal or input str...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In computing, a '''here document''' ('''here-document''', '''here-text''', '''heredoc''', '''hereis''', '''here-string''', or '''here-script''') is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.&lt;br /&gt;
&lt;br /&gt;
==File literals==&lt;br /&gt;
Narrowly speaking, here documents are file literals or stream literals. These originate in the Unix shell, though similar facilities are available in some other languages.&lt;br /&gt;
&lt;br /&gt;
===Unix shells===&lt;br /&gt;
Here documents are available in many Unix shells. In the following example, text is passed to the [[tr (Unix)|&amp;lt;code&amp;gt;tr&amp;lt;/code&amp;gt;]] command (transliterating lower to upper-case) using a here document. This could be in a shell file or entered interactively at a prompt.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ LANG=C tr a-z A-Z &amp;lt;&amp;lt; END_TEXT&lt;br /&gt;
&amp;gt; one two three&lt;br /&gt;
&amp;gt; four five six&lt;br /&gt;
&amp;gt; END_TEXT&lt;br /&gt;
ONE TWO THREE&lt;br /&gt;
FOUR FIVE SIX&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;END_TEXT&amp;lt;/code&amp;gt; was used as the delimiting identifier. It specified the start and end of the here document. The redirect and the delimiting identifier do not need to be separated by a space: &amp;lt;code&amp;gt;&amp;amp;lt;&amp;amp;lt;END_TEXT&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;amp;lt;&amp;amp;lt;&amp;amp;nbsp;END_TEXT&amp;lt;/code&amp;gt; both work equally well.&lt;br /&gt;
&lt;br /&gt;
By default, the behaviour is largely identical to the contents of double quotes: variable names are replaced by their values, commands within backticks are evaluated, etc.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ cat &amp;lt;&amp;lt; EOF&lt;br /&gt;
&amp;gt; \$ Working dir &amp;quot;$PWD&amp;quot; `pwd`&lt;br /&gt;
&amp;gt; EOF&lt;br /&gt;
$ Working dir &amp;quot;/home/user&amp;quot; /home/user&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This can be disabled by quoting any part of the label, which is then ended by the unquoted value. &amp;quot;Quoting&amp;quot; includes escaping, so if &amp;lt;code&amp;gt;\EOF&amp;lt;/code&amp;gt; is used, this is quoted, so variable interpolation does not occur, and it ends with &amp;lt;code&amp;gt;EOF&amp;lt;/code&amp;gt;, while if &amp;lt;code&amp;gt;\\EOF&amp;lt;/code&amp;gt; is used, this is quoted and ends with &amp;lt;code&amp;gt;\EOF&amp;lt;/code&amp;gt;. This perhaps surprising behaviour is however easily implemented in a shell, by the tokenizer simply recording a token was quoted (during the evaluation phase of lexical analysis), without needing to preserve the original, quoted value.&amp;lt;br/&amp;gt;&lt;br /&gt;
One application is to use &amp;lt;code&amp;gt;\'&amp;lt;/code&amp;gt; as the starting delimiter and thus &amp;lt;code&amp;gt;'&amp;lt;/code&amp;gt; as the ending delimiter, which is similar to a multiline string literal but stripping starting and ending linebreaks. The behaviour is essentially identical to that if the contents were enclosed in single quotes. Thus for example by setting it in single quotes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ cat &amp;lt;&amp;lt; 'EOF'&lt;br /&gt;
&amp;gt; \$ Working dir &amp;quot;$PWD&amp;quot; `pwd`&lt;br /&gt;
&amp;gt; EOF&lt;br /&gt;
\$ Working dir &amp;quot;$PWD&amp;quot; `pwd`&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Double quotes may also be used, but this is subject to confusion, because expansion ''does'' occur in a double-quoted string, but does ''not'' occur in a here document with double-quoted delimiter. Single- and double-quoted delimiters are distinguished in some other languages, notably [[Perl]] (see below), where behaviour parallels the corresponding string quoting.&lt;br /&gt;
&lt;br /&gt;
Appending a minus sign to the &amp;amp;lt;&amp;amp;lt; (i.e. &amp;lt;code&amp;gt;&amp;amp;lt;&amp;amp;lt;-&amp;lt;/code&amp;gt;) has the effect that leading tabs are ignored. (Not in csh or tcsh.) This allows indenting here documents in shell scripts (primarily for alignment with existing indentation) without changing their value. (Note that while tabs can typically be entered in editors, at the command line they are typically entered by &amp;lt;code&amp;gt;Ctrl-V + Tab&amp;lt;/code&amp;gt; instead, due to tab completion, and in the example, they are actual tabs, so the example can be copy and pasted.)&lt;br /&gt;
&lt;br /&gt;
A script containing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
LANG=C tr a-z A-Z &amp;lt;&amp;lt;- END_TEXT&lt;br /&gt;
Here doc with &amp;lt;&amp;lt;-&lt;br /&gt;
 A single space character (i.e. 0x20 )  is at the beginnning of this line&lt;br /&gt;
	This line begins with a single TAB character i.e 0x09  as does the next line&lt;br /&gt;
	END_TEXT&lt;br /&gt;
&lt;br /&gt;
echo The intended end was before this line &lt;br /&gt;
echo and these were not processed by tr&lt;br /&gt;
echo +++++++++++++++&lt;br /&gt;
&lt;br /&gt;
LANG=C tr a-z A-Z &amp;lt;&amp;lt; END_TEXT&lt;br /&gt;
Here doc with &amp;lt;&amp;lt;&lt;br /&gt;
 A single space character (i.e. 0x20 )  is at the beginning of this line&lt;br /&gt;
	This line begins with a single TAB character i.e 0x09 as does the next line&lt;br /&gt;
	END_TEXT&lt;br /&gt;
&lt;br /&gt;
echo The intended end was before this line, &lt;br /&gt;
echo but because the line with the delimiting Identifier began with a TAB it was NOT recognized and&lt;br /&gt;
echo the tr command continued processing.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
produces:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HERE DOC WITH &amp;lt;&amp;lt;-&lt;br /&gt;
 A SINGLE SPACE CHARACTER (I.E. 0X20 )  IS AT THE BEGINNING OF THIS LINE&lt;br /&gt;
THIS LINE BEGINS WITH A SINGLE TAB CHARACTER I.E 0X09  AS DOES THE NEXT LINE&lt;br /&gt;
The intended end was before this line&lt;br /&gt;
and these were not processed by tr&lt;br /&gt;
+++++++++++++++&lt;br /&gt;
HERE DOC WITH &amp;lt;&amp;lt;&lt;br /&gt;
 A SINGLE SPACE CHARACTER (I.E. 0X20 )  IS AT THE BEGINNNING OF THIS LINE&lt;br /&gt;
	THIS LINE BEGINS WITH A SINGLE TAB CHARACTER I.E 0X09 AS DOES THE NEXT LINE&lt;br /&gt;
	END_TEXT&lt;br /&gt;
&lt;br /&gt;
ECHO THE INTENDED END WAS BEFORE THIS LINE, &lt;br /&gt;
ECHO BUT BECAUSE THE LINE WITH THE DELIMITING IDENTIFIER BEGAN WITH A TAB IT WAS NOT RECOGNIZED AND&lt;br /&gt;
ECHO THE TR COMMAND CONTINUED PROCESSING.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another use is to output to a file:  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cat &amp;lt;&amp;lt; EOF &amp;gt; ~/testFile001&lt;br /&gt;
&amp;gt;   3 spaces precede this text.&lt;br /&gt;
&amp;gt;	A single tab character is at the beginning of this line.&lt;br /&gt;
&amp;gt;Nothing precedes this text&lt;br /&gt;
EOF&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Here strings====&lt;br /&gt;
A '''here string''' (available in bash, ksh, or zsh) is syntactically similar, consisting of &amp;lt;code&amp;gt;&amp;amp;lt;&amp;amp;lt;&amp;amp;lt;&amp;lt;/code&amp;gt;, and effects input redirection from a ''word'' (a sequence treated as a unit by the shell, in this context generally a string literal). In this case, the usual shell syntax is used for the word (&amp;quot;here string syntax&amp;quot;), with the only syntax being the redirection: a here string is an ordinary string used for input redirection, not a special kind of string.&lt;br /&gt;
&lt;br /&gt;
A single word need not be quoted:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ LANG=C tr a-z A-Z &amp;lt;&amp;lt;&amp;lt; one&lt;br /&gt;
ONE&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In case of a string with spaces, it must be quoted:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ LANG=C tr a-z A-Z &amp;lt;&amp;lt;&amp;lt; 'one two three'&lt;br /&gt;
ONE TWO THREE&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This could also be written as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ foo='one two three'&lt;br /&gt;
$ LANG=C tr a-z A-Z &amp;lt;&amp;lt;&amp;lt; &amp;quot;$foo&amp;quot;&lt;br /&gt;
ONE TWO THREE&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Multiline strings are acceptable, yielding:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ LANG=C tr a-z A-Z &amp;lt;&amp;lt;&amp;lt; 'one&lt;br /&gt;
&amp;gt; two three'&lt;br /&gt;
ONE&lt;br /&gt;
TWO THREE&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note that leading and trailing newlines, if present, are included:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ LANG=C tr a-z A-Z &amp;lt;&amp;lt;&amp;lt; '&lt;br /&gt;
&amp;gt; one&lt;br /&gt;
&amp;gt; two three&lt;br /&gt;
&amp;gt; '&lt;br /&gt;
&lt;br /&gt;
ONE&lt;br /&gt;
TWO THREE&lt;br /&gt;
&lt;br /&gt;
$&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The key difference from here documents is that, in here documents, the delimiters are on separate lines; the leading and trailing newlines are stripped. Unlike here documents, here strings do not use delimiters.&lt;br /&gt;
&lt;br /&gt;
Here strings are particularly useful for commands that often take short input, such as the calculator bc:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bc &amp;lt;&amp;lt;&amp;lt; 2^10&lt;br /&gt;
1024&lt;br /&gt;
&lt;br /&gt;
#~OR~&lt;br /&gt;
$ for i in $(seq 1 10); do bc &amp;lt;&amp;lt;&amp;lt; 2^$i; done&lt;br /&gt;
2&lt;br /&gt;
4&lt;br /&gt;
8&lt;br /&gt;
16&lt;br /&gt;
32&lt;br /&gt;
64&lt;br /&gt;
128&lt;br /&gt;
256&lt;br /&gt;
512&lt;br /&gt;
1024&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that here string behavior can also be accomplished (reversing the order) via piping and the [[echo (command)|&amp;lt;code&amp;gt;echo&amp;lt;/code&amp;gt;]] command, as in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 'one two three' | LANG=C tr a-z A-Z&lt;br /&gt;
ONE TWO THREE&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
however here strings are particularly useful when the last command needs to run in the current process, as is the case with the &amp;lt;code&amp;gt;read&amp;lt;/code&amp;gt; builtin:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 'one two three' | read -r a b c&lt;br /&gt;
$ echo &amp;quot;$a $b $c&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
yields nothing, while&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ read -r a b c &amp;lt;&amp;lt;&amp;lt; 'one two three'&lt;br /&gt;
$ echo &amp;quot;$a $b $c&amp;quot;&lt;br /&gt;
one two three&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This happens because in the previous example piping causes &amp;lt;code&amp;gt;read&amp;lt;/code&amp;gt; to run in a subprocess, and as such can not affect the environment of the parent process.&lt;br /&gt;
&lt;br /&gt;
[[Category:Linux Command Line Tools]]&lt;/div&gt;</summary>
		<author><name>Christoph</name></author>	</entry>

	</feed>