6

I've just come across the << command, used like so:

cat > outfile.txt <<EOF
Multi-line content
that will be written to outfile.txt
EOF

Now, I've no idea what this is called, but I'd quite like to know it's name, primarily so I can go and search and find out more about its syntax. Sadly, Googling for "<<" just doesn't work.

echox
  • 17,753
  • 7
  • 51
  • 56
me_and
  • 1,141
  • 1
  • 13
  • 24
  • Conveniently, I was just trying to figure out how to use it with a stdout redirection last night and couldn't get it. I kept trying to do `EOF > outfile.txt` at the end, which doesn't work well – Michael Mrozek Oct 08 '10 at 14:07
  • 2
    POSIX 2008 > Shell Command Language > Redirection > [Here-Document](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04) – jw013 Mar 23 '12 at 05:47
  • 1
    `man bash` and look for `<<` (in my `man` viewer, the search command is the `/` character, so `/<<` gets me to the right section). – jfg956 Mar 23 '12 at 08:33
  • For an experiment, you should have tried 'cat < – XTL Mar 23 '12 at 15:02
  • possible duplicate of [How does << work and what is it called?](http://unix.stackexchange.com/questions/34808/how-does-work-and-what-is-it-called) – Gilles 'SO- stop being evil' Mar 23 '12 at 18:55
  • See also [What are the shell's control and redirection operators?](http://unix.stackexchange.com/q/159513/23408) – Scott - Слава Україні Sep 11 '16 at 06:19
  • Most shells also supports `cat <<< this line is fed to cat` (a one-line here-doc) which can be handy sometimes. – olejorgenb Sep 20 '16 at 02:29

3 Answers3

17

That's called a "Here document".

http://en.wikipedia.org/wiki/Here_document

developmentalinsanity
  • 1,046
  • 1
  • 9
  • 5
8

It's form of redirection called a here document or heredoc. It redirects the contents of the given in-line document to a command. The document is delimited by the given word (EOT below). Quoting the word or part of the word after << creates a quoted here-document that the shell will not perform expansions in.

$ tac << EOT
> 123
> 456
> EOT
456
123
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Ignacio Vazquez-Abrams
  • 44,857
  • 7
  • 93
  • 100
1

In a Unix context it really is known as a "here document." I believe that the "heredoc" construct comes from PHP, Perl, and other scripting languages, and for shell scripting I'd tend to stick with "here document."

Melinda
  • 211
  • 1
  • 2