On this page
The Bash shell offers a powerful feature called "heredoc" (short for "here document") that allows you to pass multiline text or code to commands in a streamlined and readable way. Heredocs are especially useful when you need to feed multiple lines of input into a command without cluttering your script. This guide will walk you through the basics of using heredocs, along with practical examples to help you get the most out of this feature.
What is a Heredoc?
A heredoc is a block of text that you can redirect into a command, allowing you to include multiline input directly in your script. The basic syntax of a heredoc is as follows:
$ [command] << DELIMITER
multiline
input
DELIMITER
Breaking Down the Syntax
- Command: The first line begins with an optional command, followed by
<<
and a delimiter string. The delimiter marks the beginning and end of the heredoc content. - Delimiter: The delimiter can be any unique string, such as
EOF
orEND
. If the delimiter is unquoted, Bash will evaluate variables and commands within the heredoc before passing it to the command. - Multiline Input: The text or code you want to pass goes between the two instances of the delimiter. This block can include strings, variables, commands, or any other input type.
- Ending the Heredoc: The last line of the heredoc must contain the delimiter without any preceding whitespace. This tells Bash that the heredoc is complete.
Stripping Leading Whitespace
You can add a hyphen (-
) after the <<
to strip leading tabs and spaces from each line of the heredoc. This is helpful when you want to indent the heredoc text in your script for readability without affecting the actual content.
Example:
$ command <<- DELIMITER
multiline
input
DELIMITER
In this case, all leading whitespace is removed before the text is passed to the command, allowing you to format your script neatly.
Practical Examples of Using Heredocs
Now that we’ve covered the basics, let's look at some practical examples to see how heredocs can be used effectively in Bash scripts.
Outputting Multiline Text
One common use of heredocs is with the cat
command to output multiline text:
$ cat << EOF
Hello $USER!
The current working directory is $(pwd)
EOF
In this example, the heredoc passes the text block to cat
, which then prints it to the terminal. Bash performs variable substitution ($USER
) and command substitution ($(pwd)
) before passing the text to cat
.
If you want to disable these substitutions, you can quote the delimiter:
$ cat << "EOF"
Hello $USER!
The current working directory is $(pwd)
EOF
Here, everything between EOF
markers is treated as a literal string, and no substitutions occur.
To learn more about this mention check out quoting in bash.
Redirecting Output to a File
You can also redirect the output of a heredoc to a file:
$ cat << EOF > workdir.conf
The current working directory is $(pwd)
EOF
In this case, the text block is written to the file workdir.conf
instead of being printed to the terminal.
Using Heredocs for Multiline Comments
Heredocs can also be used to include multiline comments in your Bash scripts:
$ cat << COMMENT
This is a
multiline comment
COMMENT
Anything between the COMMENT
delimiters is ignored by the script, making it an easy way to add detailed comments or documentation within your code.
Conclusion
Heredocs are a valuable feature in Bash scripting, offering a straightforward way to handle multiline input. Whether you're outputting text, saving input to files, or adding comments, heredocs help you keep your scripts organized and easy to read. Experiment with heredocs in your scripts to see how they can simplify complex tasks.