What’s a doc here?

Print anything with Printful



A here document is a text list used to define input for a program, passed through a special operator. It varies in implementation across operating systems, applications, and programming languages. It allows for easy input of blocks of commands or text, and can include variables or programming code. It is useful for running programs via a script or batch file from the command line, as it allows for the same input to be used repeatedly without manual re-entry. An example is the use of a heredoc in a wrapper file for the sendmail program to automatically send an email without user input.

In relation to computers and computer programming, a here document is a text list used to define input to be passed to a program through the use of a special operator so that blocks of commands or exceptionally text can be passed more easily long. “Here document” or “heredoc” is a more generic term because the exact implementation of how a here document works and what it can contain varies from one operating system, application, or programming language to another. Despite what the name suggests, a document here is usually not a separate file, but is instead a block of text in a script or somehow typed or inserted directly at the end of a program command. The primary use for a here document comes when programs are run via a script or batch file from the command line, where heredoc provides a list of instructions for running a program or input for processing the program . Depending on many factors, a document here can be a simple list of words or it can include complex variables or programming code that the target operating system or application can decipher.

In the most basic terms, a document here is just a list of text that includes formatting characters like tabs, carriage returns, and line breaks. It’s a convenient way to pass arguments to a program that runs from the command line, especially if a wrapper file such as a script or batch file is used. Text contained in a heredoc is sent to the running program as if typed by the user exactly as it appears. This allows a single heredoc to be used over and over again to give a program the same input so that it can perform the same tasks over and over without requiring the user to re-enter everything manually.

An example of how a here document works can be seen with a simple command-line sendmail program that sends e-mail. When run, the sendmail program first prompts the user for the destination email address; then it asks for the subject of the email and, finally, the text of the email. If run from the command line, the user would respond to each prompt by typing information, hitting the Enter key, and waiting for the program to respond. When a heredoc is used in a wrapper file, all the information to be supplied to the sendmail program can be typed in advance, including return key presses, and saved in the wrapper file. This way, when the sendmail program is run with heredoc, input is automatically sent to the program and an email is sent without the user having to do anything else.




Protect your devices with Threat Protection by NordVPN


Skip to content