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
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
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
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.
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.