Reprinted from: http://my.oschina.net/u/1032146/blog/146941
What is here Document?
Here Document is a special redirection method in the Linux Shell, and its basic form is as follows
CMD << delimiter
Here Document Content
Delimiter
The function is to pass the contents of the two delimiter (here Document content part) to CMD as the input parameter;
For example, if you enter Cat << EOF in the terminal, you will be prompted to continue with the input, enter multiple lines of information and then enter EOF, and the information entered in the middle will be displayed on the screen as follows:
[Email protected]:~$ cat << EOF
> First Line
> Second Line
> Third Line EOF
> EOF
First line
Second Line
Third line EOF
Note: the ' > ' symbol is the identifier that the terminal generates prompting for input information
Here are a few things to note:
EOF is only an identity, can be replaced by any of the legal characters (the Convention is greater than the configuration);
As the end of the delimiter must shelf write, the front can not have any characters;
As the end of the delimiter can not have any characters (including the space!!!);
Spaces before and after the beginning of the delimiter are omitted;
Here document can be used not only on terminals, but also in shell files, such as the following here.sh file
Cat << EOF > Output.txt
echo "Hello"
echo "World"
Eof
Using SH here.sh to run this script file will get output.txt this new file with the following contents:
echo "Hello"
echo "World"
variants of Here document
Delimiter and Variables
In the content of here Document, you can include not only ordinary characters, but also variables in it;
For example, change the here.sh above to
Cat << EOF > output.sh
echo "This is output"
echo $
Eof
Use SH here.sh heredocument run script to get output.sh content
echo "This is output"
Echo heredocument
Here $ $ is expanded to be the parameter of the script heredocument
But sometimes you do not want to expand this variable, you can do it by adding "before and after the beginning of the delimiter", for example, change the above here.sh to
Cat << "EOF" > Output.sh #注意引号
echo "This is output"
echo $
Eof
The content of the obtained output.sh is
echo "This is output"
echo $
<< into <<-
Another use of here Document is to turn ' << ' into ' <<-'
The only change with <<-is that the tab (tab) in front of each line in the Content section of here Document will be deleted;
This usage allows you to indent the Content section while writing here document, so you can easily read the code.
The here document usage of the Linux shell