In
computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computer, computing machinery. It includes the study and experimentation of algorithmic processes, and the development of both computer hardware, hardware and softw ...
, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file
literal or
input stream
In computer science, a stream is a sequence of potentially unlimited data elements made available over time. A stream can be thought of as items on a conveyor belt being processed one at a time rather than in large batches. Streams are processe ...
literal: it is a section of a
source code
In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer.
Since a computer, at base, only ...
file that is treated as if it were a separate
file
File or filing may refer to:
Mechanical tools and processes
* File (tool), a tool used to remove fine amounts of material from a workpiece.
** Filing (metalworking), a material removal process in manufacturing
** Nail file, a tool used to gen ...
. The term is also used for a form of multiline
string literal
string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
s that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.
Here documents originate in the
Unix shell
A Unix shell is a Command-line_interface#Command-line_interpreter, command-line interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command languag ...
,
[ and are found in the ]Bourne shell
The Bourne shell (sh) is a shell command-line interpreter for computer operating systems. It first appeared on Version 7 Unix, as its default shell. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic lin ...
since 1979, and most subsequent shells. Here document-style string literals are found in various high-level language
A high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be easier to use, or may automate (or ...
s, notably the Perl programming language
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed by ...
(syntax inspired by Unix shell) and languages influenced by Perl, such as PHP
PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. ...
and Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
. JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
also supports this functionality via template literals, a feature added in its 6th revision ( ES6). Other high-level languages such as Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (prog ...
, Julia
Julia may refer to:
People
*Julia (given name), including a list of people with the name
*Julia (surname), including a list of people with the name
*Julia gens, a patrician family of Ancient Rome
*Julia (clairvoyant) (fl. 1689), lady's maid of Qu ...
and Tcl
TCL or Tcl or TCLs may refer to:
Business
* TCL Technology, a Chinese consumer electronics and appliance company
** TCL Electronics, a subsidiary of TCL Technology
* Texas Collegiate League, a collegiate baseball league
* Trade Centre Limited ...
have other facilities for multiline strings.
Here documents can be treated either as files or strings. Some shells treat them as a format string literal, allowing variable substitution and command substitution
In computing, command substitution is a facility that allows a Command-line interpreter, command to be run and its output to be pasted back on the command line as arguments to another command. Command substitution first appeared in the Bourne she ...
inside the literal.
Overview
The most common syntax for here documents, originating in Unix shells, is <<
followed by a delimiting identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, person, physical countable object (or class thereof), or physical mass ...
(often the word EOF
or END
), followed, starting on the next line, by the text to be quoted, and then closed by the same delimiting identifier on its own line. This syntax is because here documents are formally stream literals, and the content of the here document is often redirected to stdin
In computer programming, standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), ...
(standard input) of the preceding command or current shell script/executable.
The here document syntax is analogous to the shell syntax for input redirection, which is <
followed by the name of the file to be used as input.
Other languages often use substantially similar syntax, but details of syntax and actual functionality can vary significantly. When used simply for string literals, the <<
does not indicate indirection, but is simply a starting delimiter convention. In some languages, such as Ruby, <<
is also used for input redirection, thus resulting in <<
being used twice if one wishes to redirect from a here document string literal.
File literals
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.
Unix shells
Here documents are available in many Unix shells. In the following example, text is passed to the tr
command (transliterating lower to upper-case) using a here document. This could be in a shell file, or entered interactively at a prompt.
$ LANG=C tr a-z A-Z << END
> one two three
> four five six
> END
ONE TWO THREE
FOUR FIVE SIX
In this case END
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: or both work equally well.
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.
$ cat << EOF
> \$ Working dir "$PWD" `pwd`
> EOF
$ Working dir "/home/user" /home/user
This can be disabled by quoting any part of the label, which is then ended by the unquoted value; the behavior is essentially identical to that if the contents were enclosed in single quotes. Thus for example by setting it in single quotes:
$ cat << 'EOF'
> \$ Working dir "$PWD" `pwd`
> EOF
\$ Working dir "$PWD" `pwd`
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
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
(see below), where behavior parallels the corresponding string quoting.
In POSIX shell
A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to c ...
but not csh/tcsh, appending a minus sign to the (i.e. ) has the effect that leading tabs are ignored. This allows indenting here documents in shell scripts (primarily for alignment with existing indentation) without changing their value:
A script containing:
LANG=C tr a-z A-Z <<- END_TEXT
Here doc with <<-
A single space character (i.e. 0x20 ) is at the beginning of this line
This line begins with a single TAB character i.e 0x09 as does the next line
END_TEXT
echo The intended end was before this line
echo and these were not processed by tr
echo +++++++++++++++
LANG=C tr a-z A-Z << END_TEXT
Here doc with <<
A single space character (i.e. 0x20 ) is at the beginning of this line
This line begins with a single TAB character i.e 0x09 as does the next line
END_TEXT
echo The intended end was before this line,
echo but because the line with the delimiting Identifier began with a TAB it was NOT recognized and
echo the tr command continued processing.
produces:
HERE DOC WITH <<-
A SINGLE SPACE CHARACTER (I.E. 0X20 ) IS AT THE BEGINNING OF THIS LINE
THIS LINE BEGINS WITH A SINGLE TAB CHARACTER I.E 0X09 AS DOES THE NEXT LINE
The intended end was before this line
and these were not processed by tr
+++++++++++++++
HERE DOC WITH <<
A SINGLE SPACE CHARACTER (I.E. 0X20 ) IS AT THE BEGINNING OF THIS LINE
THIS LINE BEGINS WITH A SINGLE TAB CHARACTER I.E 0X09 AS DOES THE NEXT LINE
END_TEXT
ECHO THE INTENDED END WAS BEFORE THIS LINE,
ECHO BUT BECAUSE THE LINE WITH THE DELIMITING IDENTIFIER BEGAN WITH A TAB IT WAS NOT RECOGNIZED AND
ECHO THE TR COMMAND CONTINUED PROCESSING.
Another use is to output to a file:
$ cat << EOF > ~/testFile001
> 3 spaces precede this text.
> A single tab character is at the beginning of this line.
>Nothing precedes this text
EOF
Here strings
A here string (available in bash, ksh, or zsh
The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh.
Zsh was ...
) 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). In this case the usual shell syntax is used for the word (“here string syntax”), with the only syntax being the redirection: a here string is an ordinary string used for input redirection, not a special kind of string.
A single word need not be quoted:
$ LANG=C tr a-z A-Z <<< one
ONE
In case of a string with spaces, it must be quoted:
$ LANG=C tr a-z A-Z <<< 'one two three'
ONE TWO THREE
This could also be written as:
$ foo='one two three'
$ LANG=C tr a-z A-Z <<< "$foo"
ONE TWO THREE
Multiline strings are acceptable, yielding:
$ LANG=C tr a-z A-Z <<< 'one
> two three'
ONE
TWO THREE
Note that leading and trailing newlines, if present, are included:
$ LANG=C tr a-z A-Z <<< '
> one
> two three
> '
ONE
TWO THREE
$
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.
Here strings are particularly useful for commands that often take short input, such as the calculator bc
:
$ bc <<< 2^10
1024
Note that here string behavior can also be accomplished (reversing the order) via piping and the echo
In audio signal processing and acoustics, an echo is a reflection of sound that arrives at the listener with a delay after the direct sound. The delay is directly proportional to the distance of the reflecting surface from the source and the lis ...
command, as in:
$ echo 'one two three' , LANG=C tr a-z A-Z
ONE TWO THREE
however here strings are particularly useful when the last command needs to run in the current process, as is the case with the read
Read or READ may refer to:
Computing
* Read (computer), to retrieve data from a storage device
* Read (system call), a low-level IO function on a file descriptor in a computer
* Read (Unix), a command in Unix operating systems
Places
* Read, L ...
builtin:
$ echo 'one two three' , read -r a b c
$ echo "$a $b $c"
yields nothing, while
$ read -r a b c <<< 'one two three'
$ echo "$a $b $c"
one two three
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
In computing, a parent process is a process that has created one or more child processes.
Unix-like systems
In Unix-like operating systems, every process except (the swapper) is created when another process executes the fork() system call. T ...
.
Microsoft NMAKE
In Microsoft
Microsoft Corporation is an American multinational corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
NMAKE
In software development, Make is a command-line interface software tool that performs actions ordered by configured dependencies as defined in a configuration file called a ''makefile''. It is commonly used for build automation to build executa ...
, here documents are referred to as '
inline files
''. Inline files are referenced as <<
or <<pathname
: the first notation creates a temporary file, the second notation creates (or overwrites) the file with the specified pathname.
An inline file is terminated with <<
on a line by itself, optionally followed by the (case-insensitive) keyword KEEP
or NOKEEP
to indicate whether the created file should be kept.
target0: dependent0
command0 <<
temporary inline file
...
<<
target1: dependent1
command1 <<
temporary, but preserved inline file
...
<
R
R does not have file literals, but provides equivalent functionality by combining string literals with a string-to-file function. R allows arbitrary whitespace, including newlines, in strings. A string then can be turned into a file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
using the textConnection()
function. For example, the following turns a data table embedded in the source code into a data-frame variable:
str <-
"State Population Income Illiteracy Life.Exp Murder HS.Grad Frost
Alabama 3615 3624 2.1 69.05 15.1 41.3 20
Alaska 365 6315 1.5 69.31 11.3 66.7 152
Arizona 2212 4530 1.8 70.55 7.8 58.1 15
Arkansas 2110 3378 1.9 70.66 10.1 39.9 65"
x <- read.table(textConnection(str), header=TRUE, row.names=1)
Data segment
Perl and Ruby have a form of file literal, which can be considered a form of data segment
In computing, a data segment (often denoted .data) is a portion of an object file or the corresponding address space of a program that contains initialized static variables, that is, global variables and static local variables. The size of thi ...
. In these languages, including the line __DATA__
(Perl) or __END__
(Ruby, old Perl) marks the end of the code segment
In computing, a code segment, also known as a text segment or simply as text, is a portion of an object file or the corresponding section of the program's virtual address space that contains executable instructions.
Segment
The term "segment" c ...
and the start of the data segment. Only the contents prior to this line are executed, and the contents of the source file after this line are available as a file object: PACKAGE::DATA
in Perl (e.g., main::DATA
) and DATA
in Ruby. As an inline file, these are semantically similar to here documents, though there can be only one per script. However, in these languages the term "here document" instead refers to multiline string literals, as discussed below.
Data URI Scheme
As further explained in Data URI scheme
The data URI scheme is a uniform resource identifier (URI) scheme that provides a way to include data in-line in Web pages as if they were external resources. It is a form of file literal or here document. This technique allows normally separat ...
, all major web browsers understand URIs that start with ''data:'' as here document.
Multiline string literals
The term "here document" or "here string" is also used for multiline string literal
string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
s in various programming languages, notably Perl (syntax influenced by Unix shell), and languages influenced by Perl, notably PHP and Ruby. The shell-style <<
syntax is often retained, despite not being used for input redirection.
Perl-influenced
Perl
In Perl there are several different ways to invoke here docs. The delimiters around the tag have the same effect within the here doc as they would in a regular string literal: For example, using double quotes around the tag allows variables to be interpolated, but using single quotes doesn't, and using the tag without either behaves like double quotes. Using backticks as the delimiters around the tag runs the contents of the heredoc as a shell script. It is necessary to make sure that the end tag is at the beginning of the line or the tag will not be recognized by the interpreter.
Note that the here doc does not start at the tag—but rather starts on the next line. So the statement containing the tag continues on after the tag.
Here is an example with double quotes:
my $sender = "Buffy the Vampire Slayer";
my $recipient = "Spike";
print <<"END";
Dear $recipient,
I wish you to leave Sunnydale and never return.
Not Quite Love,
$sender
END
Output:
Here is an example with single quotes:
print <<'END';
Dear $recipient,
I wish you to leave Sunnydale and never return.
Not Quite Love,
$sender
END
Output:
And an example with backticks (may not be portable):
my $shell_script_stdout = <<`END`;
echo foo
echo bar
END
It is possible to start multiple heredocs on the same line:
say(<
The tag itself may contain whitespace, which may allow heredocs to be used without breaking indentation
__FORCETOC__
In the written form of many languages, indentation describes empty space ( white space) used before or around text to signify an important aspect of the text such as:
* Beginning of a paragraph
* Hierarchy subordinate concept
* Qu ...
.
say <<' END';
Hello World
END
Although since Perl version 5.26, heredocs can include indention:
#prints "Hello there\n" with no leading whitespace.
if (1)
In addition to these strings, Perl also features file literals, namely the contents of the file following __DATA__
(formerly __END__
) on a line by itself. This is accessible as the file object PACKAGE::DATA
such as main::DATA
, and can be viewed as a form of data segment
In computing, a data segment (often denoted .data) is a portion of an object file or the corresponding address space of a program that contains initialized static variables, that is, global variables and static local variables. The size of thi ...
.
PHP
In PHP, here documents are referred to as heredocs. In PHP heredocs are not string literals. Heredoc text behaves just like a double-quoted string, but without the double quotes. For example, meaning `$` will be parsed as a variable start, and `${` or `{$` as a complex variable start.
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. Otherwise, it will not be considered to be a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found, a parse error will result at the last line of the script. However, from version 7.3, it is no longer required that the closing identifier be followed by a semicolon or newline. Additionally the closing identifier may be indented, in which case the indentation will be stripped from all lines in the doc string.
In PHP 5.3 and later, like Perl, it is possible to not interpolate variables by surrounding the tag with single quotes; this is called a ''nowdoc'':
$x = <<<'END'
Dear $recipient,
I wish you to leave Sunnydale and never return.
Not Quite Love,
$sender
END;
In PHP 5.3+ it is also possible to surround the tag with double quotes, which like Perl has the same effect as not surrounding the tag with anything at all.
Ruby
The following Ruby code displays a grocery list by using a here document.
puts <
The result:
$ ruby grocery-list.rb
Grocery list
------------
1. Salad mix.
2. Strawberries.*
3. Cereal.
4. Milk.*
* Organic
The <<
in a here document does not indicate input redirection, but Ruby also uses <<
for input redirection, so redirecting to a file from a here document involves using <<
twice, in different senses:
File::open("grocery-list", "w") do , f,
f << <
As with Unix shells, Ruby also allows for the delimiting identifier not to start on the first column of a line, if the start of the here document is marked with the slightly different starter <<-
.
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:
now = Time.now
puts <<-EOF
It's #{now.hour} o'clock John, where are your kids?
EOF
Ruby expands on this by providing the "<<~" syntax for omitting indentation on the here document:
puts <<~EOF
This line is indented two spaces.
This line is indented four spaces.
This line is indented six spaces.
EOF
The common indentation of two spaces is omitted from all lines:
$ ruby indented-heredoc.rb
This line is indented two spaces.
This line is indented four spaces.
This line is indented six spaces.
Like Perl, Ruby allows for starting multiple here documents in one line:
puts <\n" + <\nAnd now it is over!"
As with Perl, Ruby features file literals, namely the contents of the file following __END__
on a line by itself. This is accessible as the file object DATA
and can be viewed as a form of data segment
In computing, a data segment (often denoted .data) is a portion of an object file or the corresponding address space of a program that contains initialized static variables, that is, global variables and static local variables. The size of thi ...
.
Python
Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (prog ...
supports multi-line strings as a "verbatim" string. They may be enclosed in 3 single (') or double (") quotation marks, the latter is shown in the examples below.
print("""
Customer: Not much of a cheese shop is it?
Shopkeeper: Finest in the district , sir.
""")
From Python 3.6 onwards, verbatim f-strings support variable and expression interpolation.
shop_type = "CHEESE"
accolade = "finest"
print(f"""
Customer: Not much of a {shop_type.lower()} shop is it?
Shopkeeper: {accolade.capitalize()} in the district , sir.
""")
Java
Text blocks are supported starting with Java 15
The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community P ...
via JEP 378:
String html = """
Hello, world
""";
C++
Since C++11
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
, C++ supports string literals with custom delimiter ("my_delimiter" in this example):
#include
const char* str = R"my_delimiter(Start of string. New line
slash \ quote " ' parens ) ( End of string)my_delimiter";
std::cout << str << std::endl;
will print out
Start of string. New line
slash \ quote " ' parens ) ( End of string
D
Since version 2.0, D has support for here document-style strings using the 'q' prefix character. These strings begin with q"IDENT
followed immediately by a newline (for an arbitrary identifier IDENT), and end with IDENT"
at the start of a line.
int main() {
string list = q"IDENT
1. Item One
2. Item Two
3. Item Three
IDENT";
writef( list );
}
D also supports a few quoting delimiters, with similar syntax, with such strings starting with q" and ending with ">/code> and ending with
or similarly for other delimiter character (any of () <> {} or []).
OS/JCL
On IBM's Job Control Language (JCL) used on its earlier MVS and current z/OS operating systems, data which is inline to a job stream can be identified by an * on a DD statement, such as
or
In the first case, the lines of text follow and are combined into a pseudo file with the DD name SYSIN. All records following the command are combined until either another OS/JCL command occurs (any line beginning with ), the default EOF
Eof (also Eoves) was a swineherd who claimed to have seen a vision of the Virgin Mary at Evesham in England, about 701. Eof related this vision to Egwin, Bishop of Worcester, who founded the great Evesham Abbey on the site of the apparition. '' ...
sequence () is found, or the physical end of data occurs. In the second case, the conditions are the same, ''except'' the DLM= operand is used to specify the text string signalling end of data, which can be used if a data stream contains JCL (again, any line beginning with ), or the sequence (such as comments in C or C++ source code). The following compiles and executes an assembly language program, supplied as in-line data to the assembler.
//AHAR JOB ('ALEX HARRIS')
// EXEC ASMLG
//SYSIN DD *
APROG START
XR 15,15
BR 14
END
/*
//* JOB ENDS
The statement is the functional equivalent of
Indicating s stream of data follows, terminated by .
Racket
Racket's here strings start with #<<
followed by characters that define a terminator for the string.
The content of the string includes all characters between the #<<
line and a line whose only content is the specified terminator. More precisely, the content of the string starts after a newline following #<<
, and it ends before a newline that is followed by the terminator.
#lang racket
(displayln
#<
Outputs:
No escape sequences are recognized between the starting and terminating lines; all characters are included in the string (and terminator) literally.
#lang racket
(displayln
#<
Outputs:
Here strings can be used normally in contexts where normal strings would:
#lang racket
(printf #<
Outputs:
An interesting alternative is to use the language extension at-exp
to write @-expressions.
They look like this:
#lang at-exp racket
(displayln @string-append{
This is a long string,
very convenient when a
long chunk of text is
needed.
No worries about escaping
"quotes" or \escapes. It's
also okay to have λ, γ, θ, ...
Embed code: @(number->string (+ 3 4))
})
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.
Windows PowerShell
In PowerShell
PowerShell is a shell program developed by Microsoft for task automation and configuration management. As is typical for a shell, it provides a command-line interpreter for interactive use and a script interpreter for automation via a langu ...
, here documents are referred to as here-strings. A here-string is a string which starts with an open delimiter (@"
or @'
) and ends with a close delimiter ("@
or '@
) on a line by itself, which terminates the string. All characters between the open and close delimiter are considered the string literal.
Using a here-string with double quotes allows variables to be interpreted, using single quotes doesn't.
Variable interpolation
Variable may refer to:
Computer science
* Variable (computer science), a symbolic name associated with a value and whose associated value may be changed
Mathematics
* Variable (mathematics), a symbol that represents a quantity in a mathemat ...
occurs with simple variables (e.g. $x
but NOT $x.y
or $x /code>).
You can execute a set of statements by putting them in $()
(e.g. $($x.y)
or $(Get-Process , Out-String)
).
In the following PowerShell code, text is passed to a function using a here-string.
The function ConvertTo-UpperCase
is defined as follows:
PS > function ConvertTo-UpperCase($string) { $string.ToUpper() }
PS > ConvertTo-UpperCase @'
>> one two three
>> eins zwei drei
>> '@
ONE TWO THREE
EINS ZWEI DREI
Here is an example that demonstrates variable interpolation and statement execution using a here-string with double quotes:
PS > $doc, $marty = 'Dr. Emmett Brown', 'Marty McFly'
PS > $time = ateTimeFriday, October 25, 1985 8:00:00 AM'
PS > $diff = New-TimeSpan -Minutes 25
PS > @"
>> $doc : Are those my clocks I hear?
>> $marty : Yeah! Uh, it's $($time.Hour) o'clock!
>> $doc : Perfect! My experiment worked! They're all exactly $($diff.Minutes) minutes slow.
>> $marty : Wait a minute. Wait a minute. Doc... Are you telling me that it's $(($time + $diff).ToShortTimeString())?
>> $doc : Precisely.
>> $marty : Damn! I'm late for school!
>> "@
Dr. Emmett Brown : Are those my clocks I hear?
Marty McFly : Yeah! Uh, it's 8 o'clock!
Dr. Emmett Brown : Perfect! My experiment worked! They're all exactly 25 minutes slow.
Marty McFly : Wait a minute. Wait a minute. Doc... Are you telling me that it's 08:25?
Dr. Emmett Brown : Precisely.
Marty McFly : Damn! I'm late for school!
Using a here-string with single quotes instead, the output would look like this:
PS > @'
>> $doc : Are those my clocks I hear?
>> $marty : Yeah! Uh, it's $($time.Hour) o'clock!
>> $doc : Perfect! My experiment worked! They're all exactly $($diff.Minutes) minutes slow.
>> $marty : Wait a minute. Wait a minute. Doc... Are you telling me that it's $(($time + $diff).ToShortTimeString())?
>> $doc : Precisely.
>> $marty : Damn! I'm late for school!
>> '@
$doc : Are those my clocks I hear?
$marty : Yeah! Uh, it's $($time.Hour) o'clock!
$doc : Perfect! My experiment worked! They're all exactly $($diff.Minutes) minutes slow.
$marty : Wait a minute. Wait a minute. Doc... Are you telling me that it's $(($time + $diff).ToShortTimeString())?
$doc : Precisely.
$marty : Damn! I'm late for school!
DIGITAL Command Language (DCL)
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. The input is either passed to the program, or can be explicitly referenced by the logical name SYS$INPUT (analogous to the Unix concept of stdin
In computer programming, standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), ...
).
For instance, explicitly referencing the input as SYS$INPUT:
$ TYPE SYS$INPUT
This text will be directly echoed
to the screen by the TYPE command.
$! other commands ...
produces:
This text will be directly echoed
to the screen by the TYPE command.
Additionally, the DECK command, initially intended for punched card support (hence its name: it signified the beginning of a data deck) can be used to supply input to the preceding command. The input deck is ended either by the command $ EOD, or the character pattern specified by the /DOLLARS parameter to DECK.
Example of a program totalling up monetary values:
$ RUN ADD_SUMS.EXE
$ DECK
$13.53
$3.33
$2.33
$ EOD
Would produce the following output (presuming ADD_SUMS was written to read the values and add them):
$19.19
Example of using DECK /DOLLARS to create one command file from another:
$ COPY SYS$INPUT SYS$SCRATCH:TEMP.COM
$ DECK /DOLLARS=$$$$
$ TYPE SYS$INPUT
This is an example of using DECK to create
a command file from within a command file
$$$$
$! other commands follow ...
YAML
YAML
YAML ( ) is a human-readable data serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Extensible Marku ...
primarily relies on whitespace indentation for structure, making it resistant to delimiter collision
A delimiter is a sequence of one or more characters for specifying the boundary between separate, independent regions in plain text, mathematical expressions or other data streams. An example of a delimiter is the comma character, which acts ...
and capable representing multi-line strings with folded string literals:
---
caption: "Example of heredoc-style functionality using YAML"
date: "2007-06-01"
example: >
HTML goes into YAML without modification
message: ,
"Three is always greater than two,
even for large values of two"
--Author Unknown
See also
*CDATA
The term CDATA, meaning character data, is used for distinct, but related, purposes in the markup languages SGML and XML. The term indicates that a certain portion of the document is general ''character data'', rather than non-character data or ch ...
*Pipeline (Unix)
In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of process (computing), processes chained together by their standard streams, so that the output text of ...
References
Notes
General
*
Bash Reference Manual
''
*
*
*
,'' Mendel Cooper
*
*
External links
Here document
Link to Rosetta Code
Rosetta Code is a wiki-based programming chrestomathy website with implementations of common algorithms and solutions to various computer programming, programming problems in many different programming languages. It is named for the Rosetta Stone ...
task with examples of here documents in over 15 languages.
{{DEFAULTSORT:Here Document
Programming constructs
String (computer science)
Articles with example C++ code
Articles with example D code
Articles with example Perl code
Articles with example PHP code
Articles with example Python (programming language) code
Articles with example Ruby code