0

I'm new to Bash scripting. I came across this link and noticed an unusual syntax for cat.

cat << !
HOPE THIS WORKS
This sample E-mail message demonstrates how one can attach
files when sending messages with the Unix sendmail utility.
!

uuencode ${file_1} ${file_1}
uuencode ${file_2} ${file_2}
uuencode ${file_3} ${file_3}
!

What does the << mean? What does the ! mean? How come the cat has an opening and closing !, but uuencode doesn't?

EDIT: Thanks for all the help! The last outstanding question I have is why is there no opening and marker for the uuencode section. From what I'm understanding, cat has a << ! indicating it's a HEREDOC. uuencode however doesn't seem like a HEREDOC. What gives?

Joseph Cho
  • 4,033
  • 4
  • 26
  • 33

1 Answers1

2

Its called heredoc, and its constructed like this:

command << MARKER
Literal text here, can contain $variables
and newlines,
    leading spaces
    and tabs... 
MARKER

And works with everything between the MARKER is going as stdin to the specified command. What marker you use is optional and in your example ! is being used. And if you feed the command cat with input on stdin it will simply print it to stdout:

cat << !
all this text will go to stdout
!
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123