A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.

除了interactive program,一些non-interactive program也支持heredoc。vi也支持heredoc。

Choose a limit string sufficiently unusual that it will not occur anywhere in the command list and confuse matters.

The closing limit string, on the final line of a here document, must start in the first character position. There can be no leading whitespace. Trailing whitespace after the limit string likewise causes unexpected behavior. The whitespace prevents the limit string from being recognized.

The - option to mark a here document limit string («-LimitString) suppresses leading tabs (but not spaces) in the output. This may be useful in making a script more readable.

limit string的前后都不要加空格。否则会遇到类似unexpected end of file之类的错误。 如果在脚本里用heredoc,为了缩进可以加上-选项(<<-LimitString),可以省略行开头的tab键。

For those tasks too complex for a here document, consider using the expect scripting language, which was specifically designed for feeding input into interactive programs.

太复杂的交互可以考虑用expect脚本来实现。