9

In bash, it seems to me both here document and here string can be used for providing strings as stdin inputs. here document may provide an extra features to specify delimiter, which I am not sure if is necessary in any case.

What are the differences between their purposes? Can one always be used wherever the other is used? When shall we use which? Thanks.

Tim
  • 98,580
  • 191
  • 570
  • 977
  • 6
    Use here-strings all the time if you wish, until you encounter a situation where it's not possible. Then use a here-document. Or use temporary files, always, and never ever touch here-strings nor here-documents. Deciding when to use what is something you as a developer has to do. – Kusalananda Nov 18 '18 at 16:21
  • I don't see why this question got closed. Good to know I'm not the only one subjected to bizarre mod intervention. – Sridhar Sarnobat May 12 '22 at 07:10

1 Answers1

9

Here string is for shorter strings, here document for longer ones. The extra feature of here documents is that you can pass arbitrary text without having to worry about quoting. Just single-quote the delimiter, and make sure it does not show up in the text. You may use a random string (drawing from characters and digits), a technique that is also used to delimit parts in MIME encoded email.

stefan
  • 1,081
  • 1
  • 7
  • 16