Learn something new every day
More Info... by email
In relation to computers and computer programming, a here document is a list of text used to define input to be passed to a program through the use of a special operator so exceptionally long blocks of commands or text can be passed more easily. "Here document", or "heredoc", is more of a general term because the exact implementation of how a here document operates and what it can contain will vary from one operating system, application or programming language to the next. Despite what the name implies, a here document is not usually a separate file, but instead is a block of text in a script or somehow typed or inserted directly on the end of a program command. The main use for a here document comes when programs are executed through a script or batch file from the command line, where the heredoc provides either a list of instructions for a program to execute or input for the program to process. Depending on many factors, a here document can be a simple list of words or it can include complex variables or programming code that the operating system or target application can decipher.
In the most basic terms, a here document is just a list of text that includes formatting characters such as tabs, carriage returns and line breaks. It is a convenient way to pass arguments to a program that is being executed from the command line, especially if a wrapper file such as a script or batch file is used. The text contained in a heredoc is sent to the executing program as if it were being typed in 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 it can perform the same tasks repeatedly without requiring the user to actually re-input everything manually.
One example of how a here document works can be seen with a simple command line sendmail program that sends email. When executed, the sendmail program first asks the user to enter the destination email address; it then asks for the subject of the email and, finally, the text of the email. If executed from the command line, the user would respond to each prompt by typing in the information, hitting the return key and waiting for the program to respond. When a heredoc is used in a wrapper file, all the information to be given to the sendmail program can be typed out in advance, including the return key presses, and saved in the wrapper file. In this way, when the sendmail program is executed with the heredoc, the input automatically is fed to the program and an email is sent without requiring the user to do anything more.