Louis I. answered 04/26/19
Computer Science Instructor/Tutor: Real World and Academia Experienced
What you're asking about is called a "here document" ...
it's a very useful feature of all UNIX/Linux shells. It's considered a type of IO redirection that allows the developer to create a code/script block that gets passed into an interactive program: such as
- ftp / telnet
- sqlplus
- mail (or any command-line email client)
- any script interpreter (like python)
- cat ( cat? why cat??)
So the motivation to use cat in this context is not so obvious?
What does "cat" do? Fundamentally, it echos out what ever it reads from stdin or files.
Executing "cat" with no parameters, and proceeding to type "hello" and then "there" at it will yield the following experience:
$ cat
hello
hello
there
there
So we can use cat within a here document to generate some specified multi-lined text content on demand ...
$ cat <<EOF
> Close your eyes and I'll kiss you
> Tomorrow I'll miss you
> Remember I'll always be true
> And then while I'm away
> I'll write home every day
> And I'll send All My Loving to you
> EOF
Close your eyes and I'll kiss you
Tomorrow I'll miss you
Remember I'll always be true
And then while I'm away
I'll write home every day
And I'll send All My Loving to you
We can even intermix shell substitute-able values - like this:
$ cat <<EOF
> My HOME directory on $(hostname)
> is $HOME
> EOF
My HOME directory on lji-laptop
is /home/lji
The basic structure of a here document is as follows:
interactive-command <<START_AND_TOKEN
line 1 to be processed by interactive-command
line 2 to be processed by interactive-command
...
line N to be processed by interactive-command
START_AND_TOKEN
Here Documents are quite often placed within shell functions - deferring their execution, and making them ultimately re-usable ... for example:
function sendAlert () {
mailx -s"Alert" $DISTRO_LIST <<-MAILER
Hello, all - the following file systems are above
their utilization high-water-mark of ${TOP_LIMIT}:
$FILE_SYSTEMS_IN_TROUBLE
MAILER
}