We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What Is a Here Document?

By Eugene P.
Updated: May 17, 2024
References

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.

WiseGeek is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Link to Sources
Discussion Comments
Share
https://www.wisegeek.net/what-is-a-here-document.htm
WiseGeek, in your inbox

Our latest articles, guides, and more, delivered daily.

WiseGeek, in your inbox

Our latest articles, guides, and more, delivered daily.