1

From the Advanced Bash-Scripting Guide:

The special symbol << ... is similar to interactive-program < command-file

In fact, everything I can think to do with the << here-document symbol, I can already do simply with < command-file or <<< 'list of commands'. Namely, I can do

cat < <(echo fdsa
echo asdf)

or

cat <<< 'fdsa
asdf'

instead of

cat << a
fdsa
asdf
a

One (obvious) exception is, in case you want your command list to terminate differently, depending on what terminating parameter you pass in. However, I cannot think of any situations where that might be useful.

Another exception is, that using a here-document allows you to place special characters, such as ', into your output without worrying about how to escape them. However, this too, seems like more a convenience than an essential feature.

Where else might here-documents be so essential, that Unix reserved a special operator for them?

7

Consider non-Bash shells that do not have Bash's here-strings (<<<"...") or process substitutions (<(...)), and the fact that some people need to write scripts that exhibits a level of portability between systems.

Here-documents are used whenever a multi-line pre-formatted piece of text needs to be shipped to a utility, possibly with or without performing variable substitution etc. on its contents.

In a script, this may be useful to present the user with some text, for example:

To avoid the tedious

echo 'Usage:'
echo "    $0 [-a|-b] file [file ...]"
echo
echo 'Options:'

(See a real world example of someone actually doing this, in a book, and try to keep track of which strings are evaluated and which ones are not. It wouldn't pass my code review, for sure: https://books.google.com/books?id=0LvYSCi7QsIC&pg=PA201)

Instead, one may simply feed a here-document to cat:

cat <<-END_USAGE
        Usage:
            $0 [-a|-b] file [file ...]

        Options:
END_USAGE

This, incidentally, also provides an opportunity to do a certain amount of documentation of the script by intelligent choice of here-document "tag" (END_USAGE, END_FTP, etc.).

It's also useful for sending complete scripts to utilities such as ftp or other programs that are able to read command sequences on their standard input.

As a Bash programmer, you are obviously free to use Bash's way of doing things, but I do think that here-documents still provides a cleaner way of sending a pre-formatted document to a command than using a long and rambling string.

Removing here-documents from Bash would also render Bash useless as a candidate for /bin/sh on most Unix systems, as it would seriously cripple its POSIX conformance.

| improve this answer | |
  • So basically, the support for << is more common than that for <( and <<<? Also-- what do you mean by This, incidentally, also provides an opportunity to do a certain amount of documentation of the script by intelligent choice of here-document "tag" (END_USAGE, END_FTP, etc.).? – Alex Aug 25 '17 at 19:12
  • @Alex 1) Yes, definitely. Here-strings and process substitutions are not standardized and some shells do net implement them at all. 2) Just the fact that your script is annotated with an end tag for the here document tells the reader what the contents of the document is about, so it's easier to e.g. find the usage info in the code, or the ftp command script or whatever it is. – Kusalananda Aug 25 '17 at 19:18
  • <<< comes from zsh. Or more exactly from zsh and the Unix port of rc. The authors of their respective shells were exchanging ideas in the early days (early 90s), it's unclear whether the idea came from Byron Rakitzis or Paul Falstad or others, but both zsh and Byron's rc had it in 1991 and not in 1990. It was only added much later to ksh93 and bash (2002, 2.05b) and in 2.7 in yash. << itself was probably first introduced to Unix by Steve Bourne in the late 70s though the idea (and possibly syntax) likely predates that. <(...) comes from ksh in the mid-80s. – Stéphane Chazelas Aug 25 '17 at 20:42
  • And you may also mention <<- which can not be replaced with a multi-line string. – xhienne Aug 25 '17 at 20:47
  • @xhienne, <<- added in the SysIII shell in 1981. – Stéphane Chazelas Aug 25 '17 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.