Here document
lastmod 2024-06-20created 2020-12-293 edits
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 source code that is treated as if it were a separate file. The term is also used for a form of multiline string literal that uses similar syntax, preserving line breaks and other whitespace (including indentation) in the text.
Here documents originate in the Unix shell, and are found in sh, csh, Tcsh, ksh, bash, zsh, and others. Here document-style string literals are found in various high-level programming languages, notably the Perl programming language (whose syntax was influenced by the Unix shell) and languages influenced by Perl (e.g. PHP, Korean: 루비). Other high-level languages such as Korean: 파이썬 and Tcl provide other facilities for multiline strings.
A here document can be treated either as a file or as a string. Some shells treat them as a Printf literal, allowing variable substitution and command substitution inside the literal.
The most common here document syntax, originating in Unix shells, is << followed by a delimiting identifier (often EOF or END), followed on the next line by the quoted text, and then closed by the same delimiting identifier. This syntax means that the here document is a stream literal, and the content of the document is redirected to the stdin (standard stream) of the preceding command. The here document syntax is analogous to the syntax for input redirection, <, meaning "take input from the following file".
Other languages often use similar syntax, but details of the syntax and actual functionality can differ significantly. When used simply for string literals, << does not indicate indirection but may simply mean the starting delimiter. In some languages, such as Ruby, << is also used for input redirection, so << must be used twice if you want to redirect from a here document string literal.