tags:

views:

142

answers:

5

Could someone explain the "<<" in the following code?

mysql test<<E0Q
Select * from signins

I'd try to search for it myself, but symbols are hard to search for...

Thanks, Dan

+8  A: 

It's a "here file", see http://www.linuxjournal.com/article/3771

Davide
+3  A: 

Here docs, or a way of easily piping large text blocks into a program.

mipadi
+2  A: 

They are called here-documents. From the manual:

Here Documents

This type of redirection instructs the shell to read input from the
current source until a line containing only word (with no trailing blanks)  is
seen.   All  of  the lines read up to that point are then used as the standard
input for a command.

The format of here-documents is:

       <<[-]word
               here-document
       delimiter
Roberto Bonvallet
+3  A: 

It's not only used for piping though. For example, the scripts used in the Linux From Scratch walkthroughs make extensive use of heredocs in combination with the cat command and the output redirection operator (>). Here is an example of such a thing:

user@domain ~$ cat >test.c <<EOF
int main(void){return 0;}
EOF
user@domain ~$

That writes all of the text between the <<EOF start delimiter and the EOF end delimiter to a file `test.c' and once the EOF end delimiter is encountered, you're returned to the shell prompt.

Dustin
Why is this better than: echo "int main(void){return 0;}">test.c? If I understand you correctly it is the same thing.
Dan
It isn't better or worse; it's merely an example. However, suppose you wanted multiple lines and/or used strings with quotes. Arguments in favour of using heredocs are mainly based around those two things because a heredoc is often used like a "super quote" to allow you to mix " and ' characters when necessary. It's especially advantageous to use it in that manner because of the '!' character inside double quotation marks seems to trigger an error about an event not being found. As a result, single quotes must be used, though useful functionality such as variable expansion is lost.
Dustin
The primary benefit of a here document is that it is much more concise than a sequence of echo commands for a long document. As part of that benefit, it also avoids the overheads of one echo process per line.
Dale Hagglund
A: 

If you pipe echo into read, for example, the variable assignment is "lost" since read gets executed in a subshell. You can use a here-doc or a here-string to make it execute in the current shell:

read -a var <<< "some text"

That's an example of another construct you may see called a "here-string", which is similar to a here-doc.

See this and this.

Dennis Williamson