The final concept we'll introduce in this chapter is the here document. Here documents, also called heredocs, are used to supply input to certain commands, slightly different to stdin redirection. Notably, it is an easy way to give multiline input to a command. It works with the following syntax:
cat << EOF
input
more input
the last input
EOF
If you run this in your Terminal, you'll see the following:
reader@ubuntu:~/scripts/chapter_12$ cat << EOF
> input
> more input
> the last input
> EOF
input
more input
the last input
The << syntax lets Bash know you want to use a heredoc. Right after that, you're supplying a delimiting identifier. This might seem complicated, but it really means that you supply a string that will terminate the input. So, in our example, we supplied the commonly used EOF (short for end of file).
Now, if the heredoc encounters a line in the input that exactly matches the delimiting identifier, it stops listening for further input. Here's another example that illustrates this more closely:
reader@ubuntu:~/scripts/chapter_12$ cat << end-of-file
> The delimiting identifier is end-of-file
> But it only stops when end-of-file is the only thing on the line
> end-of-file does not work, since it has text after it
> end-of-file
The delimiting identifier is end-of-file
But it only stops when end-of-file is the only thing on the line
end-of-file does not work, since it has text behind it
While using this with cat illustrates the point, it is not a very practical example. The wall command, however, is. wall lets you broadcast a message to everyone connected to the server, to their Terminal. When used in combination with a heredoc, it looks a little like this:
reader@ubuntu:~/scripts/chapter_12$ wall << EOF
> Hi guys, we're rebooting soon, please save your work!
> It would be a shame if you lost valuable time...
> EOF
Broadcast message from reader@ubuntu (pts/0) (Sat Nov 10 16:21:15 2018):
Hi guys, we're rebooting soon, please save your work!
It would be a shame if you lost valuable time...
In this case, we receive our own broadcast. If you connect multiple times with your user, however, you'll see the broadcast come in there as well.
Give it a try with a Terminal console connection and an SSH connection simultaneously; you'll understand it much better if you see it first-hand.