When writing shell scripts you may be in a situation where you need to pass a multiline block of text or code to an interactive command, such as [tee](https://linuxize.com/post/linux-tee-command/)
, cat
, or [sftp](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/)
.
In Bash and other shells like Zsh, a Here document (Heredoc) is a type of redirection that allows you to pass multiple lines of input to a command.
The syntax of writing HereDoc takes the following form:
[COMMAND] <<[-] 'DELIMITER'
HERE-DOCUMENT
DELIMITER
Copy
<<
and the delimiting identifier.<<-
, will cause all leading tab characters to be ignored. This allows you to use indentation when writing here-documents in shell scripts. Leading whitespace characters are not allowed, only tab.In this section, we will look at some basic examples of how to use heredoc.
Heredoc is most often used in combination with the cat command.
#bash #bash heredoc #ssh