Here document

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.

JavaScript also supports this functionality via template literals, a feature added in its 6th revision (ES6).

The most common syntax for here documents, originating in Unix shells, is << followed by a delimiting identifier (often the word EOF or END[2]), followed, starting on the next line, by the text to be quoted, and then closed by the same delimiting identifier on its own line.

[1] In the following example, text is passed to the tr command (transliterating lower to upper-case) using a here document.

By default, behavior is largely identical to the contents of double quotes: variable names are replaced by their values, commands within backticks are evaluated, etc.

[4] Single- and double-quoted delimiters are distinguished in some other languages, notably Perl (see below), where behavior parallels the corresponding string quoting.

In POSIX shell but not csh/tcsh, appending a minus sign to the << (i.e. <<-) has the effect that leading tabs are ignored.

[5] This allows indenting here documents in shell scripts (primarily for alignment with existing indentation) without changing their value:[c] A script containing: produces: Another use is to output to a file: A here string (available in bash, ksh, or zsh) is syntactically similar, consisting of <<<, and effects input redirection from a word (a sequence treated as a unit by the shell, in this context generally a string literal).

A single word need not be quoted: In case of a string with spaces, it must be quoted: This could also be written as: Multiline strings are acceptable, yielding: Note that leading and trailing newlines, if present, are included: The key difference from here documents is that, in here documents, the delimiters are on separate lines; the leading and trailing newlines are stripped.

Here strings are particularly useful for commands that often take short input, such as the calculator bc: Note that here string behavior can also be accomplished (reversing the order) via piping and the echo command, as in: however here strings are particularly useful when the last command needs to run in the current process, as is the case with the read builtin: yields nothing, while This happens because in the previous example piping causes read to run in a subprocess, and as such cannot affect the environment of the parent process.

The term "here document" or "here string" is also used for multiline string literals in various programming languages, notably Perl (syntax influenced by Unix shell), and languages influenced by Perl, notably PHP and Ruby.

This is accessible as the file object PACKAGE::DATA such as main::DATA, and can be viewed as a form of data segment.

Outputs In PHP versions prior to 7.3, the line containing the closing identifier must not contain any other characters, except an optional ending semicolon.

If a proper closing identifier is not found, a parse error will result at the last line of the script.

Besides, Ruby treats here documents as a double-quoted string, and as such, it is possible to use the #{} construct to interpolate code.

The following example illustrates both of these features: Ruby expands on this by providing the "<<~" syntax for omitting indentation on the here document: The common indentation of two spaces is omitted from all lines: Like Perl, Ruby allows for starting multiple here documents in one line: As with Perl, Ruby features file literals, namely the contents of the file following __END__ on a line by itself.

The //SYSIN DD * statement is the functional equivalent of <

[15] They look like this: Outputs: An @-expression is not specific nor restricted to strings, it is a syntax form that can be composed with the rest of the language.

The function ConvertTo-UpperCase is defined as follows: Here is an example that demonstrates variable interpolation and statement execution using a here-string with double quotes: Using a here-string with single quotes instead, the output would look like this: In DCL scripts, any input line which does not begin with a $ symbol is implicitly treated as input to the preceding command - all lines which do not begin with $ are here-documents.

Example of a program totalling up monetary values: Would produce the following output (presuming ADD_SUMS was written to read the values and add them): Example of using DECK /DOLLARS to create one command file from another: YAML primarily relies on whitespace indentation for structure, making it resistant to delimiter collision and capable representing multi-line strings with folded string literals: